Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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