Ruby on Rails 3 - 404路由

Pau*_*aul 1 http-status-code-404 ruby-on-rails-3

我正试图在rails中设置我的404 ...

我遵循了这些指示

如果做的事情似乎有效:

www.myapp.com/no_controller/

但如果我这样做:

www.myapp.com/existing_controller/no_action

我得到了活动记录,记录未找到......等等......

我希望这也可以路由到404页面...这可能吗?

Vol*_*ldy 9

当你去

www.myapp.com/existing_controller/no_action
Run Code Online (Sandbox Code Playgroud)

你居然打电话show的动作existing_controllerno_action作为id.在开发模式下,您会收到RecordNotFound错误.在制作中,您将获得404页面.

如果你想在开发模式下自定义这个行为并根目录到404页面(顺便说一下我不建议它!因为它是故意帮助你调试的),你可以rescue_from犯这个错误:

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
Run Code Online (Sandbox Code Playgroud)

超出范围.在设计404页面时,此示例中的一种技术非常有用:与标准不同,public/404.html您可以使用此方法使用应用程序布局.