我阅读了本指南的大部分内容 http://guides.rubyonrails.org/getting_started.html很久以前。
我设置了一个文章 Controller ,当我创建一篇新文章时,会生成一个新的数字,例如
https://www.zzz.com/articles/1
https://www.zzz.com/articles/2
https://www.zzz.com/articles/3
我刚刚注意到,如果我输入一个不存在的数字,我会收到一条错误消息
ActiveRecord::RecordNotFound in ArticlesController#show
然后它显示我不希望人们看到的信息,例如
Rails.root: /home/myname/myapp
如何只显示 404 错误?
我试过将这样的东西添加到应用程序 Controller ,但它没有帮助:
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
# Render 404 page when record not found
def render_404
render :file => "#{RAILS_ROOT}/public/404.html", :status => 404
end
end
我目前在开发模式而不是生产模式下运行。这重要吗?
最佳答案
是的,以开发
模式而不是生产
模式运行很重要。因为在 development
中,应用程序 hat 其他默认值和 try 被优化以帮助开发人员,例如通过显示错误消息。
production
中的默认值是不同的,并且针对在生产服务器上运行的应用程序进行了优化:应用程序将运行得更快,并且不会向浏览器发送错误消息。
如果您想测试该行为,您可以将 config/environments/development.rb
中的 consider_all_requests_local
设置为 false
(参见Rails docs about configuration)。这将禁止向浏览器发送调试信息。
但是,我建议在生产
环境中的服务器上运行应用程序。
关于ruby-on-rails - 如何防止 Rails 在找不到带有 'id' = 的文章时显示个人信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46387302/