5

how to render 404 instead NoMethodError in People#show error

the code

def show
  @person = Person.find(params[:id])
   respond_to do |format|
    format.html # show.html.erb
    format.xml  { render :xml => @person }
  end
end
rails_noob
  • 311
  • 2
  • 5
  • 14
  • See: http://stackoverflow.com/questions/2385799/how-to-redirect-to-a-404-in-rails/4983354#4983354 – Kelvin Aug 12 '11 at 21:27

1 Answers1

10

NoMethodError will render a 500 in production mode, however if you want to also render a 404 status in the headers in development mode, you can do the following:

 redirect_to :status => 404

To render the standard 404 page, you can check out the top answer here.

Community
  • 1
  • 1
Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
  • oh.nice. NoMethodError will render a 404 in production mode – rails_noob Mar 22 '11 at 01:15
  • I'm sorry, it actually renders a 500 error. Either way, it's more graceful than what you see in development mode. – Mike Lewis Mar 22 '11 at 01:33
  • so,how to render 404 ?i dont want my user see 500 error page. – rails_noob Mar 22 '11 at 01:38
  • You can see the link I included in the answer. Basically you try catch the error and render a 404 as seen in the link above. – Mike Lewis Mar 22 '11 at 01:40
  • what im doing is put `rescue_from ActiveRecord::RecordNotFound do render '404' end` in application_controller. but is this the correct way?is this how the rails handle recordnotfound error? – rails_noob Mar 22 '11 at 02:55