简体   繁体   English

rails3 link_to:具有属性?

[英]rails3 link_to :with attribute?

i am wondering if the :with attribute is removed from rails3 since i cannot find anything in the rails3 api - http://rails3api.s3.amazonaws.com 我想知道:with属性是否已从rails3中删除,因为我无法在rails3 api中找到任何内容-http : //rails3api.s3.amazonaws.com

anyone has a clue or give a hint on how to use the :with parameter to send data with a link_to 任何人都有线索或提示如何使用:with参数通过link_to发送数据

non-working example: 非工作示例:

= link_to "Foo", {:action => "filter", :filter => "filter1",:with => "'test='+$('search').value"}, :remote => true, :class => "trash unselected", :id => "boo"

thanks! 谢谢!

I was struggling with this today and after a while I came up with little hack in rails.js. 我今天在为此苦苦挣扎,一段时间后,我想到了rails.js的一些小技巧。 In handleRemote method I've changed this: 在handleRemote方法中,我对此进行了更改:

} else {
    method = element.attr('data-method');
    url = element.attr('href');
    data = null;
}

to this: 对此:

} else {
    method = element.attr('data-method');
    url = element.attr('href');
    data = eval(element.attr('data-with'));
}

Thank's to that now I can use link_to :remote like this: 谢谢,现在我可以像这样使用link_to:remote了:

<%= link_to "link", some_path, :remote => true, 'data-with' => "'address=' + $j('#office_address').val();" %>

NOTE: this is only valid if you are using jquery, but it shouldn't be difficult to apply this for prototype 注意:这仅在使用jquery时有效,但是将其应用于原型应该不难

This goes against the point of non-obstrusive javascript and this is why it has been removed. 这与非干扰性javascript的观点背道而驰,这就是为什么将其删除的原因。 Try looking at the railscast about the subject here: http://railscasts.com/episodes/205-unobtrusive-javascript 尝试在此处查看有关该主题的railscast: http ://railscasts.com/episodes/205-unobtrusive-javascript

You should try another way of doing this. 您应该尝试另一种方式。

There is though a way to work around it. 尽管有一种解决方法。

Use this in the view as a guideline: 在视图中将此用作指导:

link_to "Foo", {:action => "filter", :filter => "filter1"}, {:remote => true, :class => "trash unselected", :id => "boo", 'data-with' => "'&test='+$('search').value"}

(moved :with to the second part and made it 'data-with') (将:with移至第二部分,并将其设为“数据与”)

and add this to the bottom: 并将其添加到底部:

<script type="text/javascript" charset="utf-8">
  $$('a[data-remote=true]').each(function(a){
    a.onclick = function(){a.setAttribute('href', a.getAttribute('href') + eval(a.getAttribute('data-with')))};
  });
</script>

of course you will need to have prototype loaded (is in the default javascripts of a rails app) 当然,您需要加载原型(在Rails应用的默认javascript中)


For something a better than this one-liner: http://gist.github.com/451508 对于比这种单线更好的东西: http : //gist.github.com/451508

Using the gist you don't need to start :with with & or ? 使用要点,您无需以&或?开始:

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

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