开发者

super frustrated about rescue_action_in_public

开发者 https://www.devze.com 2023-04-03 10:22 出处:网络
I\'ve been spending a lot of time hunting around on google trying to figure out how to simply get rails to display my errors as if I am in production mode (simply so I can verify that it does as I am

I've been spending a lot of time hunting around on google trying to figure out how to simply get rails to display my errors as if I am in production mode (simply so I can verify that it does as I am expecting)...

So first I read that you could set config/environments/development.rb to have

config.consider_all_requests_local       = false

And then in the application controller do:

protected

def local_request?
  false
end

def rescue_action_in_public(exception)
  logger.info("**** LOL ****")
  redirect_to :root, :notice => "lol"
end

However, triggering an ActiveRecord::RecordNotFound error still displays a stacktrace and it does not put anything in my log.

... next I read that you have to be in production mode... So I tried that.. Same thing..

Then I read, that you need to replace the local_request? method in application controller with one in config/environments/production.rb

开发者_如何学编程
class ActionDispatch::Request
  def local_request?(*args)
    false
  end
end

So I did that.. still get the stack trace, and no log message..

Then I read that rescue_action_in_public has to be in the ShowExceptions class.. So I put that in config/initializers/error_handling.rb

module ActionDispatch
  class ShowExceptions

    protected

    def rescue_action_in_public(exception)

      logger.info("************** WTF ")

      status = status_code(exception).to_s

      template = ActionView::Base.new(["#{Rails.root}/app/views"])
      if ["404"].include?(status)
        file = "/errors/404.html.erb"
      else
        file = "/errors/500.html.erb"
      end
      body = template.render(:file => file)

      render(status, body)
    end

  end
end

Still.. I get a stack trace.. and still nothing in my log.


Just run the application in production mode:

$ rails server -e production

Everything should work normal. Errors are also logged in your log/production.log (the logs will appear only after you stop the server).

In order for everything to work, you may first need to precompile your assets with

$ rake assets:precompile

and change

config.serve_static_assets = true

in your config/environments/production.rb.

0

精彩评论

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