Use default route in rails

I am trying to remove the catch-all or default route from the industrial rails application. I am interested in supporting the work of collecting a log of its use so that I can replace it with the corresponding hard-coded routes.

So, given that I have the following default route line in my config/routes.rb .

 match '/:controller(/:action(/:id))' 

How can I create or retrieve a log every time this route hits. This log should ideally include only requests actually processed by this route along with the parameters, and it will need to leave the route itself as normal.

+6
source share
2 answers

One way to do this is to change the default route:

 match ':controller(/:action(/:id))(.:format)', :using_default_route => true 

Then put the following function in app/controllers/application_controller.rb

 before_filter do if params[:using_default_route] logger.info("Default route for #{request.path.inspect}. params = #{params.inspect}") end end 
+7
source

Another possibility would be to use the Rails router constraints parameter:

 match '/:controller(/:action(/:id))', constraints: -> (req) { Rails.logger.info("Default route used: #{req.path.inspect}") true } 

Note: lambda returns true for the match to succeed.

+8
source

Source: https://habr.com/ru/post/910286/


All Articles