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