Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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 (?)

like image 638
Harry Wood Avatar asked Jun 29 '11 15:06

Harry Wood


1 Answers

Try

<%= link_to "product list", { :controller => "product", :action => "list" }, "data-role" => "button" %>
like image 193
Dogbert Avatar answered Nov 07 '22 14:11

Dogbert