Skip to content
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 1 changed file with 7 additions and 6 deletions.
@@ -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.

Copy link
@rubys

rubys Dec 7, 2012

Contributor

Why the extra blank line?

This comment has been minimized.

Copy link
@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
Copy link
Member

@mislav mislav commented on 339e4e8 Dec 7, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@awj
Copy link

@awj awj commented on 339e4e8 Dec 7, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

@rubys rubys commented on 339e4e8 Dec 7, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member

@jeremy jeremy commented on 339e4e8 Dec 7, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Member Author

@dhh dhh commented on 339e4e8 Dec 7, 2012 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.