添加自定义数据属性rails图像标记

时间:2021-01-19 16:58:43

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:

我需要在我的rails应用程序中为galleria添加数据描述和数据标题,但我看不到如何使用image标签执行此操作。到目前为止我有这个:

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

但这会引发未定义的局部变量或方法`description'错误,那么我如何在rails 3中执行此操作?

2 个解决方案

#1


44  

The correct syntax for this is

正确的语法是

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

#2


0  

As of rails 5.1, this syntax should work:

从rails 5.1开始,这种语法应该有效:

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

阅读以下示例中有关您可以包含在image_tag中的属性的更多信息:rails api docs。

#1


44  

The correct syntax for this is

正确的语法是

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

#2


0  

As of rails 5.1, this syntax should work:

从rails 5.1开始,这种语法应该有效:

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

阅读以下示例中有关您可以包含在image_tag中的属性的更多信息:rails api docs。