Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to trace redirect_to in a rails app while hunting bugs

While debugging an issue, its hard to trace a redirect_to in a rails application when you have a lot of gems and before_filters. So is there a way by which i can trace them faster? any methods or rails helpers available for that?

like image 960
Magesh Avatar asked Mar 07 '13 14:03

Magesh


1 Answers

add the below method in your application controller

def redirect_to(options = {}, response_status = {})
  ::Rails.logger.error("Redirected by #{caller(1).first rescue "unknown"}")
  super(options, response_status)
end

and then you will be able to see the file, method name that invoked the 'redirect_to' call in the log file (it will be logged)

eg:

Started GET "http://server.com/really_important_page" for 127.0.0.1
Processing by HomeController#really_important_page as HTML
Redirected by app/controllers/application_controller.rb:53:in `ensure_random_bugs'
Redirected to https://server.com/not_the_page_you_wanted
Completed 302 Found in 1ms

This saved my day so wanted to share here, hope you find it useful :)

like image 147
Magesh Avatar answered Oct 01 '22 20:10

Magesh