21

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?

2
  • 1
    Can you wrap those in :html => {}? Commented Feb 25, 2013 at 12:23
  • I couldn't get this working in Rails 2 but good thinking. Commented Mar 5, 2017 at 9:38

2 Answers 2

50

The correct syntax for this is

<%= image_tag entry.filename, :title => "title", :class => "class", :data => { :description => entry.caption, :title => entry.caption }  %>
Sign up to request clarification or add additional context in comments.

1 Comment

Why isn't that specified in the docs? api.rubyonrails.org/classes/ActionView/Helpers/…
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.