How to add a custom attribute to the HTML output of rails link_to
With rails version 2.3.8 I'm trying to get HTML output like:
<a href="/product/list" data-role="button">Product list</a>
So an A tag with a custom attribute "data-role". (The reason I want the "data-role" attribute is that I'm working with JQuery Mobile which picks up on this attribute and does its magic) So I was trying to do this
<%= link_to "product list", :controller => "product", :action => "list", "data-role" => "button" %>
Judging by this tutorial perhaps this would work in rails 3, but in rails 开发者_StackOverflow中文版2.3.8 it interprets it by generating a data-role=button parameter on the link URL.
One way I can get the desired HTML output is to use url_for instead:
<a href="<%=url_for :controller =>"product", :action => "list" %>" data-role="button">Product list</a>
A bit long and ugly. Is there a way to make link_to output custom attributes in the A tag (?)
Try
<%= link_to "product list", { :controller => "product", :action => "list" }, "data-role" => "button" %>
I use fancybox in link_to on rails.
<%= 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 %>
Output:
<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>
精彩评论