当用户输入的网址不正确时,引发的理想错误是什么?

gsm*_*oza 5 ruby-on-rails

我有一个基于url参数搜索记录的操作.该动作的URL看起来像这样:

http://domain.com/records/filter/<filtercode>
Run Code Online (Sandbox Code Playgroud)

如果用户输入了错误的过滤代码,我想申请

  • 引发错误,以便Hoptoad向我报告错误.
  • 在生产环境中渲染404而不是500.

我知道某些Rails错误,如ActiveRecord :: NotFound和ActionController:RoutingError将在生产环境中呈现404.所以我喜欢做的是当用户在网址中输入无效的过滤码时引发此错误.

现在,我的问题是:在这种情况下提出的理想错误是什么?网络中是否有Rails错误/异常列表?

tyb*_*103 5

您正在寻找

ActionController::RoutingError
Run Code Online (Sandbox Code Playgroud)

我喜欢在我的 application_controller 中这样做:

  def not_found
    raise ActionController::RoutingError.new('Not Found')
  end
Run Code Online (Sandbox Code Playgroud)

然后在其他控制器中,例如 pages#show,我可以这样做:

def show
  unless @page = Page.find_by_permalink(params[:permalink])
    not_found
  end
end
Run Code Online (Sandbox Code Playgroud)


nit*_*der 2

如果它搜索记录并且没有找到任何合乎逻辑的方法是使用 ActiveRecord::NotFound 不是吗?