Rails link_to assigning class and id

故事扮演 提交于 2019-12-10 03:59:45

问题


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


回答1:


Does this work for you?

<%= link_to event, id: "an-id", class: "some-class" do  %>
  #bunch of stuff making up the partial.
<% end %>



回答2:


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>"), ....)  %>


来源:https://stackoverflow.com/questions/14284633/rails-link-to-assigning-class-and-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!