Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use urlhelper to include rails 3 custom data- attribute

I am using Rails 3 and found that if I add :remote => :true, there will be added to the tag the data-remote = true attribute. But I can't find a way to add custom data- attributes to the urlhelper. The followings won't work:

<%= link_to projects_path, :history => "new"%>
<%= link_to projects_path, :data-history => "new"%> #this throws an error
<%= link_to projects_path, :data_history => "new"%>

What I want to generate is: New Project

anyone?

like image 435
Nik So Avatar asked Sep 07 '10 18:09

Nik So


2 Answers

What about:

<%= link_to 'New Project', new_project_path, 'data-history' => 'new' %> 

( http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to )

like image 127
sled Avatar answered Nov 02 '22 23:11

sled


This is an elegant solution:

<%= link_to "foo", foo_path, data: { foo: "bar" } %>
like image 31
dangerousdave Avatar answered Nov 03 '22 01:11

dangerousdave