Skip to content

Instantly share code, notes, and snippets.

@wlangstroth
Last active December 28, 2015 16:58
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 wlangstroth/7532161 to your computer and use it in GitHub Desktop.
Save wlangstroth/7532161 to your computer and use it in GitHub Desktop.
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