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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.