Darme
Darme

Reputation: 7083

How to bypass default_url_options?

In my last Rails project I had to overwrite default_url_options in application_controller.rb to always pass a param through every request, like this:

def default_url_options 
  { :my_default_param => my_param_value }
end  

Now every url helper attaches a my_default_param at the end of every generated url. For instance user_url(@current_user) generates a url like:

http://www.example.com/users/1?&my_default_param=my_param_value 

But sometimes I need to generate the url without my_default_param. Is there some option I can pass to the url helper so that user_url(@current_user, some_option: some_option_value) will return only:

http://www.example.com/users/1?&my_default_param=my_param_value

?

PN: I already tried with :overwrite_params=> { } but it doesn't work, probably because it's evaluated before default_url_options.

And if not, is there another way to obtain the @current_user url without the my_default_param attached?

Thank you.

Upvotes: 4

Views: 1816

Answers (1)

ABrukish
ABrukish

Reputation: 1452

default_url_options has one param options which you could use as you wish:

def default_url_options( options = {} )

  use_defaults = options.delete( :use_defaults )

  if use_defaults
    { :my_default_param => 'my_param_value' }
  else
    {}
  end

end

Or you could reverse the logic:

def default_url_options( options = {} )

  ignore_defaults = options.delete( :ignore_defaults )

  if ignore_defaults
    {}
  else
    { :my_default_param => 'my_param_value' }
  end

end

Upvotes: 4

Related Questions