开发者

Ruby on Rails 3 - 404 routing

I am trying to setup 开发者_如何学编程my 404s in rails...

I have followed these instructions

and its seems to work if do do something like:

www.myapp.com/no_controller/

but if I do:

www.myapp.com/existing_controller/no_action

I get the Active Record, record not found...etc...

I would like that to also route to the 404 page...is this possible?


When you go to

www.myapp.com/existing_controller/no_action

you actually call show action of existing_controller with no_action as id. In the development mode you get RecordNotFound error. In the production you will get a 404 page.

If you want to customize this behavior in the development mode and root to the 404 page (BTW I don't suggest it! because it was done intentionally to help you debug), you can rescue_from this error:

class ApplicationController < ActionController::Base

  rescue_from ActiveRecord::RecordNotFound do
    render_404
  end 

  def render_404
    respond_to do |type|
      type.html { render :template => "shared/error_404/message", :layout => "application", :status => "404 Not Found" }
      type.all  { render :nothing  => true, :status => "404 Not Found" }
    end
  end
end

Out of the scope. One technique in this example can be useful when designing 404 pages: unlike standard public/404.html you can use application layout with this approach.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜