Last active
June 1, 2018 14:59
-
-
Save eric1234/f79ce5a156f8fc6707c1273ea2cd5f18 to your computer and use it in GitHub Desktop.
Lettable-based Controller Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class WidgetsController < ApplicationController | |
let(:widgets) { Widget.all } | |
let(:widget) { widgets.find_or_initialize_by id: params[:id] } | |
def new | |
render :form | |
end | |
def edit | |
render :form | |
end | |
def create | |
save | |
end | |
def update | |
save | |
end | |
def destroy | |
widget.destroy | |
redirect_to widgets_url, notice: 'Widget was successfully destroyed.' | |
end | |
private | |
def save | |
if widget.update secure_params | |
redirect_to widget, notice: 'Widget was successfully saved.' | |
else | |
render :form | |
end | |
end | |
def secure_params | |
params.require(:widget).permit :name | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment