Permalink
Browse files

Let the scaffold example use the "set shared record" pattern to expla…

…in callbacks
  • Loading branch information...
dhh committed Dec 7, 2012
1 parent 8d02afe commit 339e4e80d514bd94fcb0e15689db43e5de83642a
Showing with 7 additions and 6 deletions.
  1. +7 −6 railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb
@@ -4,6 +4,8 @@
<% end -%>
<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApplicationController
+ before_action :set_<%= singular_table_name %>, except: [ :index, :new, :create ]
+
# GET <%= route_url %>
# GET <%= route_url %>.json
def index
@@ -18,8 +20,6 @@ def index
# GET <%= route_url %>/1
# GET <%= route_url %>/1.json
def show
- @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
-
respond_to do |format|
format.html # show.html.erb
format.json { render json: <%= "@#{singular_table_name}" %> }
@@ -39,7 +39,6 @@ def new
# GET <%= route_url %>/1/edit
def edit
- @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
end
# POST <%= route_url %>
@@ -61,8 +60,6 @@ def create
# PATCH/PUT <%= route_url %>/1
# PATCH/PUT <%= route_url %>/1.json
def update
- @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
-
respond_to do |format|
if @<%= orm_instance.update_attributes("#{singular_table_name}_params") %>
format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> }
@@ -77,7 +74,6 @@ def update
# DELETE <%= route_url %>/1
# DELETE <%= route_url %>/1.json
def destroy
- @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
@<%= orm_instance.destroy %>
respond_to do |format|
@@ -86,7 +82,12 @@ def destroy
end
end
+

This comment has been minimized.

Show comment
Hide comment
@rubys

rubys Dec 7, 2012

Contributor

Why the extra blank line?

@rubys

rubys Dec 7, 2012

Contributor

Why the extra blank line?

This comment has been minimized.

Show comment
Hide comment
@frodsan

frodsan Dec 10, 2012

Contributor

Removed here a3c29b7

@frodsan

frodsan Dec 10, 2012

Contributor

Removed here a3c29b7

private
+ # Use callbacks to share common setup or constraints between actions.
+ def set_<%= singular_table_name %>
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
+ end
# Use this method to whitelist the permissible parameters. Example: params.require(:person).permit(:name, :age)
# Also, you can specialize this method with per-user checking of permissible attributes.

5 comments on commit 339e4e8

@mislav

This comment has been minimized.

Show comment
Hide comment
Member

mislav replied Dec 7, 2012

@awj

This comment has been minimized.

Show comment
Hide comment
@awj

awj Dec 7, 2012

Nice!

I've untangled my fair share of messy and/or flat out wrong lookup logic that got that way by being reimplemented in a ton of controller actions.

awj replied Dec 7, 2012

Nice!

I've untangled my fair share of messy and/or flat out wrong lookup logic that got that way by being reimplemented in a ton of controller actions.

@rubys

This comment has been minimized.

Show comment
Hide comment
@rubys

rubys Dec 7, 2012

Contributor

I'm not a fan. An example where this actually makes things harder: http://intertwingly.net/projects/AWDwR4/checkdepot-187-32/section-10.2.html.

Contributor

rubys replied Dec 7, 2012

I'm not a fan. An example where this actually makes things harder: http://intertwingly.net/projects/AWDwR4/checkdepot-187-32/section-10.2.html.

@jeremy

This comment has been minimized.

Show comment
Hide comment
@jeremy

jeremy Dec 7, 2012

Member

before

  # GET /carts/1
  # GET /carts/1.json
  def show
    begin
      @cart = Cart.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      logger.error "Attempt to access invalid cart #{params[:id]}"
      redirect_to store_url, :notice => 'Invalid cart'
    else
      respond_to do |format|
        format.html # show.html.erb
        format.json { render :json => @cart }
      end
    end
  end

after

  before_action :set_cart

  def show
    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @cart }
    end
  end

  private
    def set_cart
      unless @cart = Cart.find_by_id(params.require(:id))
        logger.error "Attempt to access invalid cart #{params[:id]}"
        redirect_to store_url, :notice => 'Invalid cart'
      end
    end

IMO that's an improvement - keeps the action focused on crafting the response.

Member

jeremy replied Dec 7, 2012

before

  # GET /carts/1
  # GET /carts/1.json
  def show
    begin
      @cart = Cart.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      logger.error "Attempt to access invalid cart #{params[:id]}"
      redirect_to store_url, :notice => 'Invalid cart'
    else
      respond_to do |format|
        format.html # show.html.erb
        format.json { render :json => @cart }
      end
    end
  end

after

  before_action :set_cart

  def show
    respond_to do |format|
      format.html # show.html.erb
      format.json { render :json => @cart }
    end
  end

  private
    def set_cart
      unless @cart = Cart.find_by_id(params.require(:id))
        logger.error "Attempt to access invalid cart #{params[:id]}"
        redirect_to store_url, :notice => 'Invalid cart'
      end
    end

IMO that's an improvement - keeps the action focused on crafting the response.

@dhh

This comment has been minimized.

Show comment
Hide comment
@dhh

dhh Dec 7, 2012

Member
Member

dhh replied Dec 7, 2012

Please sign in to comment.