【问题标题】:How to add a custom attribute to the HTML output of rails link_to如何将自定义属性添加到 rails link_to 的 HTML 输出
【发布时间】:2011-09-25 06:39:34
【问题描述】:

使用 rails 2.3.8 版,我正在尝试获取 HTML 输出,例如:

    <a href="/product/list" data-role="button">Product list</a>

所以一个带有自定义属性“data-role”的 A 标记。 (我想要“data-role”属性的原因是我正在使用JQuery Mobile,它利用了这个属性并发挥了它的魔力)所以我试图这样做

    <%= link_to "product list", :controller => "product", :action => "list", "data-role" => "button" %>

根据this tutorial 判断,这可能在 rails 3 中有效,但在 rails 2.3.8 中,它通过在链接 URL 上生成 data-role=button 参数来解释它。

获得所需 HTML 输出的一种方法是改用 u​​rl_for:

    <a href="<%=url_for :controller =>"product", :action => "list" %>" data-role="button">Product list</a> 

有点长而且丑。有没有办法让link_to在A标签中输出自定义属性(?)

【问题讨论】:

    标签: ruby-on-rails jquery-mobile


    【解决方案1】:

    试试

    <%= link_to "product list", { :controller => "product", :action => "list" }, "data-role" => "button" %>
    

    【讨论】:

    • 很高兴能找到一种方法来改变 link_to 以始终生成带有按钮数据角色的链接
    • 我来不及了,但现在你可以使用 rails html helpers。
    【解决方案2】:

    我在 link_to on rails 中使用了 fancybox。

    <%= link_to image_path('website/website_new/apps/filter_2.png'),"data-fancybox"  => "images" do %>
      <%= image_tag "website/website_new/apps/filter_2.png", class:"image-medium inline-block cast-shadow" %>
    <% end %>
    

    输出:

    <a data-fancybox="images" href="/assets/website/website_new/apps/filter_1-d6e69e3728465146276c548645b649be.png"><img class="image-medium inline-block cast-shadow" src="/assets/website/website_new/apps/filter_1-d6e69e3728465146276c548645b649be.png" alt="Filter 1"></a>
    

    【讨论】:

      猜你喜欢
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-18
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多