Reputation: 690
I am currently trying to add a custom data attribute to a Rails image_tag to allow my jQuery to function.
First, I am starting with an image_tag like so:
<%= image_tag("randomv1.png", class: "icon js-icon") %>
Second, for my jQuery, I need the image_tag to trigger an event, and I want it to include a data attribute like the one I am using for a button here:
<button class="icon js-icon" data-icon-text="Random">Icon graphic</button>
Adding the class attribute is easy, but I will need the data-icon-text to function as an attribute too for the data to be displayed after a .hover.
Based on this previous question, I tried to modify the image_tag:
<%= image_tag("randomv1.png", :class "icon js-icon" :data => { :icon-text => "Random" }) %>
Since this is delivering an error, can I create that custom attribute?
Upvotes: 0
Views: 1104
Reputation: 7779
:icon-text
is not a valid ruby name, just use :icon_text
and rails will do the magic
for you, also the arrow =>
is missed for class
attribute
<%= image_tag("randomv1.png", :class => "icon js-icon" :data => { :icon_text => "Random" }) %>
You can also use the same format as your example:
<%= image_tag("randomv1.png", class: "icon js-icon" data: { icon_text: "Random" }) %>
Edit (Correction):
<%= image_tag("randomv1.png", class: "icon js-icon", data: { icon_text: "Random" }) %>
Upvotes: 0
Reputation: 5105
Try to do this:
You have to revised the class need =>
. Then, you have to change :icon-text
into string "icon-text"
.
<%= image_tag("randomv1.png", :class => "icon js-icon", :data => { "icon-text" => "Random" }) %>
or
<%= image_tag("randomv1.png", :class => "icon js-icon", "data-icon-text" => "Random") %>
I hope this help you
Upvotes: 4