satish
satish

Reputation: 943

How do I create a link_to tag with both class and data fields?

I want to create a Rails link_to tag that will ultimately spawn a dialog box and so it just link to "#". However, I would like it to have a class and data attribute. But when I try this

<%= link_to "What's This?", "#", {class: "more_info"}, :data => { :more_info => 'mt_hashes_info' } %>

I get the error

wrong number of arguments (given 4, expected 0..3)

What's the right way to construct this link?

Upvotes: 1

Views: 2129

Answers (3)

fool-dev
fool-dev

Reputation: 7777

You can do this simply like this

<%= link_to "What's This?", "#", class: "more_info", data: { more_info: "mt_hashes_info" } %>

the generated HTML

<a class="more_info" data-more-info="mt_hashes_info" href="#">What's This?</a>

With confirmation dialog

<%= link_to "What's This?", "#", class: "more_info", data: { confirm: "Are you sure?" } %>

the generated HTML

<a class="more_info" data-confirm="Are you sure?" href="#">What's This?</a>

You can find more the link_to

Upvotes: 1

Yechiel K
Yechiel K

Reputation: 538

This is what it should look like:

<%= link_to "What's This?", "#", {:class => "more_info", :data => { :more_info => 'mt_hashes_info' }} %>

Class and Data both go into the same options hash.

Upvotes: 2

Phlip
Phlip

Reputation: 5343

Here's an example from the heart of Redmine:

link_to(l(:button_archive), archive_project_path(project, :status => params[:status]), :data => {:confirm => l(:text_are_you_sure)}, :method => :post, :class => 'icon icon-lock')

Sorry about the run-on statement; you know Redmine! But note both :data and :class are peers of the same Hash...

Upvotes: 0

Related Questions