Add the default value to the prefix to my routes generated by * _path methods

I added each line of map.resources to the route file prefix value. So, it looks like this:

map.resources :subjects, :path_prefix => ':company'

I even added this line for default behavior

map.connect ':company/:controller/:action/:id'

which is not necessary (I believe) because all routes are processed using the resource method.

I get the: company parameter in my before_filter method in ApplicationController. Everything is working. But.

Is it possible to change the behavior of all * _path methods so that it defaults to the company value for all generated URLs using the value taken from the URL? For it to work perfectly, I would have to add the company value as a parameter for each _path method. I believe that this can be done automatically.

+3
source share
1 answer

You must overwrite default_url_options in your application controller.

class ApplicationController > ActionController::Base

  # ...

  def default_url_options(options)
    { :company => current_company.id }
  end

end
+2
source

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


All Articles