Rails link_to assigning class and id

2019-04-26 20:07发布

<%= 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?

2条回答
\"骚年 ilove
2楼-- · 2019-04-26 20:42

Does this work for you?

<%= link_to event, id: "an-id", class: "some-class" do  %>
  #bunch of stuff making up the partial.
<% end %>
查看更多
叼着烟拽天下
3楼-- · 2019-04-26 20:53

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>"), ....)  %>
查看更多
登录 后发表回答