【问题标题】:Rails - how can I create an ajax link for row highlightingRails - 如何为行突出显示创建 ajax 链接
【发布时间】:2012-08-20 05:34:02
【问题描述】:

我有一个很好的即时行高亮示例: http://www.mrc-productivity.com/techblog/?p=684

如何使用 rails 创建<a href="#" class="on">Turn on row highlights</a> 链接。

我尝试了许多 link_to 和 button_to 与 :remote => true 的组合,但我无法正确使用语法,而且一切似乎都以常规链接结束。

在一天结束时,我试图打开和关闭行表突出显示,如下所示:

html:

<a href="#" class="on">Turn on row highlights</a>

jquery:

$(document).ready(function() {
$("a.on").click(function(){
    $("tr:nth-child(odd)").addClass("two");
    $("tr:nth-child(even)").addClass("one");
    event.preventDefault();
    });

    $("a.off").click(function(){
        $("tr:nth-child(odd)").removeClass("two");
        $("tr:nth-child(even)").removeClass("one");
    });
});

【问题讨论】:

    标签: ruby-on-rails ruby link-to


    【解决方案1】:

    您可以轻松地创建这样的链接:

    link_to('Turn on row highlights', '#', :class => 'on')
    

    【讨论】:

      【解决方案2】:

      在这种情况下,您根本不需要使用 Rails 助手。您可以直接在 erb 文件中使用该链接标签:

      <a href="#" class="on">Turn on row highlights</a>
      

      或者对于 HAML:

      %a.on{href: '#'} Turn on row highlights
      

      Rails 视图助手仅在您链接到 Rails 资源时有用。当您想使用 AJAX 不显眼地访问资源时,使用 remote: true 选项。不适用于您的情况。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-12-24
        • 2018-04-19
        • 2019-04-01
        • 2013-01-20
        • 2013-09-14
        • 1970-01-01
        • 2011-08-31
        • 2018-07-25
        相关资源
        最近更新 更多