Skip to content

Instantly share code, notes, and snippets.

@eric1234
Last active June 1, 2018 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eric1234/f79ce5a156f8fc6707c1273ea2cd5f18 to your computer and use it in GitHub Desktop.
Save eric1234/f79ce5a156f8fc6707c1273ea2cd5f18 to your computer and use it in GitHub Desktop.
Lettable-based Controller Example
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