…in callbacks
- Loading branch information
There are no files selected for viewing
5 comments
on commit 339e4e8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
endafter
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
endIMO that's an improvement - keeps the action focused on crafting the response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.

Why the extra blank line?