代码之家  ›  专栏  ›  技术社区  ›  z3cko

Rails3链接到:具有属性?

  •  2
  • z3cko  · 技术社区  · 16 年前

    我想知道是否从rails3中删除了:with属性,因为在rails3 api中找不到任何东西- http://rails3api.s3.amazonaws.com

    任何人都有线索或提示如何使用:with参数发送带有链接的数据

    非工作示例:

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

    谢谢!

    3 回复  |  直到 16 年前
        1
  •  1
  •   marcgg    16 年前

    这与非排外javascript的观点背道而驰,这就是为什么它被删除了。试着看看关于这个主题的铁路广告: http://railscasts.com/episodes/205-unobtrusive-javascript

    你应该试试别的办法。

        2
  •  2
  •   Adrian Serafin    15 年前

    今天我正努力解决这个问题,过了一会儿我在rails.js中发现了一些小技巧。在handleremote方法中,我更改了以下内容:

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

    对此:

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

    谢谢,现在我可以使用link_to:remote这样:

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

    注意:这仅在使用jquery时有效,但将其应用于prototype并不困难

        3
  •  1
  •   Kostas    16 年前

    不过,有办法解决这个问题。

    在视图中使用此作为准则:

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

    (移动:与第二部分并使其成为“数据与”)

    并将其添加到底部:

    <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>
    

    当然,您需要加载prototype(在rails应用程序的默认javascripts中)


    为了比这一行更好的东西: http://gist.github.com/451508

    用你不需要开始的要点:用&还是?

    推荐文章