Lon*_*Guy 2 ruby rubygems ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1
用户可以像这样访问其他用户个人资料:
site.com/username
我的用户控制器中的find方法将通过用户名查找用户..现在,如果用户不存在,我希望将它们重定向到我将制作的自定义404错误页面.
我还希望他们重新定向他们输入的任何其他不存在的网址.
我在google上找到了一些解决方案,但是想知道是否有人可以在rails 3.2中给我一个最新的例子,因为我发现这些教程是pre rails 3.1.
亲切的问候
我用Rails 3.2尝试了这个解决方案,它只是工作.
https://makandracards.com/makandra/12807-custom-error-pages-in-rails-3-2
在你的 config/application.rb
config.exceptions_app = self.routes
Run Code Online (Sandbox Code Playgroud)在你的 config/routes.rb
get '/404', to: 'errors#not_found'
get '/500', to: 'errors#server_error'
Run Code Online (Sandbox Code Playgroud)创建 app/controllers/errors_controller.rb
class ErrorsController < ApplicationController
def not_found
render status: 404
end
def server_error
render status: 500
end
end
Run Code Online (Sandbox Code Playgroud)创建app/views/errors/not_found.html.haml和app/views/errors/server_error.html.haml(取代haml的erb,如果你不使用HAML)
重启服务器
| 归档时间: |
|
| 查看次数: |
2518 次 |
| 最近记录: |