简体   繁体   中英

Adding an application-wide default parameter to link_to generated links

I want to alter the link_to functionality in that way, that it appends a default parameter to all links that are generated within the page.

eg current behavior.

link_to "foo", "/" generates <a href="http://www.example.com/">foo</a>

i want to specify in the application controller (or somewhere else central) something, that makes link_to behave like follows

link_to "foo", "/" generates <a href="http://www.example.com/?layout=foo">foo</a>

i know i could do it by adding the parameter to link_to, but i want to alter the behavior application-wide without having to edit every single link_to call.

is it possible to catch the call to link_to, append a parameter and then return the result?

current workaround i am using is to add the parameter using jquery to the href of the links.. but thats not very pretty actually

You can override link_to helper method, and pass parameter of your own choice. As I have passed parameter layout=foo in following code.

 def link_to(text='',link='#',options={}) link = link.include?('?') ? "#{link}&layout=foo" : "#{link}?layout=foo" super(text,link,options) 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM