Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding custom data attributes rails image tag

Tags:

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?

like image 773
Dean Avatar asked Feb 25 '13 12:02

Dean


2 Answers

The correct syntax for this is

<%= image_tag entry.filename, :title => "title", :class => "class", :data => { :description => entry.caption, :title => entry.caption }  %> 
like image 83
jvnill Avatar answered Nov 28 '22 11:11

jvnill


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.

like image 23
Jake Avatar answered Nov 28 '22 11:11

Jake