Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails link_to assigning class and id

<%= link_to event do  %>
  #bunch of stuff making up the partial.
<% end %>

So I'm trying to assign an ID and a class to each item in a partial. I've seen where you have to call the full link_to function like <%= link_to event, { controller: :controller, action: :action }, {class: 'someClass', id: 'someId' } %>.

That's not working for me, because of the do block, methinks? Ideas?

like image 994
Dudo Avatar asked Jan 11 '13 18:01

Dudo


2 Answers

You can do

<%= link_to 'event', { controller: :pages, action: :home }, class: 'someClass', id: 'someId'  %>

which will give you

<a href="/the_generated_path" class="someClass" id="someId">event</a>

To make 'event' actually an HTML div you can do

<%= link_to(raw("<div>..</div>"), ....)  %>
like image 149
Sully Avatar answered Nov 17 '22 07:11

Sully


Does this work for you?

<%= link_to event, id: "an-id", class: "some-class" do  %>
  #bunch of stuff making up the partial.
<% end %>
like image 21
Ben Avatar answered Nov 17 '22 08:11

Ben