Adding custom data attributes rails image tag

放肆的年华 提交于 2019-12-20 11:34:16

问题


I need to add data-description and data-title for galleria in my rails application but I can't see how to do this with the image tag. So far I have this:

<div id="galleria">
<% @entries.each do |entry| %>
     <%= image_tag entry.filename, :title => "title", :class => "class", :data-description => entry.caption, :data-title => entry.caption  %>
 <% end %>
 </div>

But this raises the undefined local variable or method `description' error, so how would I do this in rails 3?


回答1:


The correct syntax for this is

<%= image_tag entry.filename, :title => "title", :class => "class", :data => { :description => entry.caption, :title => entry.caption }  %>



回答2:


As of rails 5.1, this syntax should work:

<%= image_tag entry.filename, title: "title", class: "class", data: {description: entry.caption, title: entry.caption} %>

Read more about attributes you can include in your image_tag under examples: rails api docs.



来源:https://stackoverflow.com/questions/15066830/adding-custom-data-attributes-rails-image-tag

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