Last active
October 9, 2015 06:17
-
-
Save GBH/3451944 to your computer and use it in GitHub Desktop.
Textmate snippet `controller`. Scope `source.ruby`
This file contains 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 ${1:Model}sController < ApplicationController | |
before_action :build_${1/./\l$0/}, only: [:new, :create] | |
before_action :load_${1/./\l$0/}, only: [:show, :edit, :update, :destroy] | |
def index | |
@${1/./\l$0/}s = ${1:Model}.page(params[:page]) | |
end | |
def show | |
render | |
end | |
def new | |
render | |
end | |
def edit | |
render | |
end | |
def create | |
@${1/./\l$0/}.save! | |
flash[:success] = '${1:Model} created' | |
redirect_to action: :show, id: @${1/./\l$0/} | |
rescue ActiveRecord::RecordInvalid | |
flash.now[:error] = 'Failed to create ${1:Model}' | |
render action: :new | |
end | |
def update | |
@${1/./\l$0/}.update_attributes!(${1/./\l$0/}_params) | |
flash[:success] = '${1:Model} updated' | |
redirect_to action: :show, id: @${1/./\l$0/} | |
rescue ActiveRecord::RecordInvalid | |
flash.now[:error] = 'Failed to update ${1:Model}' | |
render action: :edit | |
end | |
def destroy | |
@${1/./\l$0/}.destroy | |
flash[:success] = '${1:Model} deleted' | |
redirect_to action: :index | |
end | |
protected | |
def build_${1/./\l$0/} | |
@${1/./\l$0/} = ${1:Model}.new(${1/./\l$0/}_params) | |
end | |
def load_${1/./\l$0/} | |
@${1/./\l$0/} = ${1:Model}.find(params[:id]) | |
rescue ActiveRecord::RecordNotFound | |
flash[:error] = '${1:Model} not found' | |
redirect_to action: :index | |
end | |
def ${1/./\l$0/}_params | |
params.fetch(:${1/./\l$0/}, {}).permit(:attributes) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment