rails 3 app in production raises 500 error when record not found?
When I go to a bogus url like:
/posts/99999999
On my local machine, I see:
ActiveRecord::RecordNotFound in Posts#show
Showing /Users/patrick/rails/my_app/app/views/posts/show.html.haml where line #1 raised:
Couldn't find Post with ID=99999
... The log shows:
Rendered posts/show.html.haml within layouts/application (29.6ms)
Completed in 423ms
ActionView::Template::Error (Couldn't find Post with ID=99999)
--- However, when I do this in production, I get the public/500.html error page, not 404... I'm开发者_运维知识库 kind of confused because in development mode, the browser shows ActiveRecord::RecordNotFound (which should mean 404, yes?) but the log shows ActionView::Template::Error-- not one mention of ActiveRecord::RecordNotFound.
So, ultimately, my question is, how can I make this a 404 not a 500? And-- why is it a 500?
From what I have seen, Rails should automatically make an ActiveRecord::RecordNotFound render the 404.html page... The problem I discovered was I am using the gem decent_exposure, and because of it's nature of lazy loading, it causes the error to happen in the view rather than the controller, and therefore Rails doesn't know to render the 404 page because it shows up as an actual ActionView::Template::Error...
Just add a !
to the find_by_column
method.
Example:
@post = Post.find_by_id!(params[:id])
Then, a RecordNotFound
exception is thrown.
精彩评论