Module: ControllerExtensions::UrlExt

Included in:
ActionController::Base
Defined in:
lib/controller_extensions/url_ext.rb

Instance Method Summary collapse

Instance Method Details

#url_for(options = {}) ⇒ Object

override default url_for method. Add ability to set default params.

Example:

'auctions_path' return '/auctions' by default
if you set @hash_of_additional_params = {:test => 1, my_param => 2} 'auctions_path' will return '/auctions?test=1&my_param=2'

You can use before_filer to do this stuff automatically. Example:

in HomeController:
 before_filter { @hash_of_additional_params = {:test => '1'} } #this will add test param to all urls in home views


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/controller_extensions/url_ext.rb', line 15

def url_for(options = {})
  options = case options
              when String
                uri = Addressable::URI.new
                uri.query_values = @hash_of_additional_params
                options + (options.index('?').nil? ? '?' : '&') + "#{uri.query}"
              when Hash
                options.reverse_merge(@hash_of_additional_params || {})
              else
                options
            end

  super
end