Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jack-style controller
class ThingsController < ActiveController
before_action :build_thing, only: [:new, :create]
before_action :load_thing, only: [:show, :edit, :update, :destroy]
def index
@thing = Thing.page(params[:page])
end
def show
render
end
def new
render
end
def create
@thing.save!
flash[:success] = 'Thing created'
redirect_to things_path
rescue ActiveRecord::RecordInvalid
render :new
end
def update
@thing.update_attributes!(thing_params)
flash[:success] = 'Thing updated'
redirect_to things_path
rescue ActiveRecord::RecordInvalid
render :edit
end
def edit
render
end
def destroy
@team_photo.destroy
flash[:success] = 'Thing deleted'
redirect_to things_path
end
protected
def build_thing
@thing = Thing.new(thing_params)
end
def load_thing
@thing = Thing.find(params[:id])
rescue ActiveRecord::RecordNotFound
flash[:error] = 'Thing not found'
redirect_to things_path
end
def thing_params
params.fetch(:thing, {}).permit(
:attribute
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.