Rails routing: Giving default values for path helpers

后端 未结 2 1257
梦如初夏
梦如初夏 2021-02-06 13:31

Is there some way to provide a default value to the url/path helpers?

I have an optional scope wrapping around all of my routes:

#config/routes.rb
Foo::A         


        
相关标签:
2条回答
  • 2021-02-06 13:40

    I think you will want to do something like this:

    class ApplicationController < ActionController::Base
    
      def url_options
        { :current_brand => @current_brand }.merge(super)
      end
    
    end
    

    This method is called automatically every time url is constructed and it's result is merged into the parameters.

    For more info on this, look at: default_url_options and rails 3

    0 讨论(0)
  • 2021-02-06 13:52

    In addition to CMW's answer, to get it to work with rspec, I added this hack in spec/support/default_url_options.rb

    ActionDispatch::Routing::RouteSet.class_eval do
      undef_method :default_url_options
      def default_url_options(options={})
        { :current_brand => default_brand }
      end
    end
    
    0 讨论(0)
提交回复
热议问题