简体   繁体   English

在link_to生成的链接中添加应用程序范围的默认参数

[英]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. 我想以这种方式更改link_to功能,它会将默认参数附加到页面内生成的所有链接。

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的行为如下

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. 我知道我可以通过向link_to添加参数来做到这一点,但是我想在应用程序范围内更改行为,而不必编辑每个link_to调用。

is it possible to catch the call to link_to, append a parameter and then return the result? 是否可以捕获对link_to的调用,附加一个参数,然后返回结果?

current workaround i am using is to add the parameter using jquery to the href of the links.. but thats not very pretty actually 我正在使用的当前解决方法是使用jquery将参数添加到链接的href中。

You can override link_to helper method, and pass parameter of your own choice. 您可以覆盖link_to帮助方法,并传递自己选择的参数。 As I have passed parameter layout=foo in following code. 由于我在以下代码中传递了参数layout=foo

 def link_to(text='',link='#',options={}) link = link.include?('?') ? "#{link}&layout=foo" : "#{link}?layout=foo" super(text,link,options) end 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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