Rails - Link_To Helper with Data-* Attribute

Rails - link_to helper with data-* attribute

Just pass them in... Rails has a default :data hash

= link_to body, url, :data => { :foo => 'bar', :this => 'that' }

One gotcha - you must surround symbols with quotes if they include a dash:

:data => { :'foo-bar' => 'that' }

Update: In Rails 4, underscores are automatically converted to dashes, so you can do this:

:data => { :foo_bar => 'that' }

Alternatively you can just write it directly:

= link_to body, url, :'data-foo' => 'bar', :'data-this' => 'that'

Update 2: As pointed out in the comments, Ruby 1.9+ allows this syntax, which is now the preferred formatting:

{ data: { foo: "bar" } }

Data attribute with no value in Rails link helper

<%= link_to "Section 2", "#", data: {section_title: true } %>

Will generate the following code:

<a href="#" data-section-title="true">Section 2</a>

That should work.

Using my own HTML5 data attributes in a Rails link_to helper

Apparently when using this format, I needed to specify the controller and action in a hash.

<%= link_to "Login", {:controller => "access", :action => "login"}, 'data-placement' => 'below' %>

Data attributes inside a link_to helper method in Rails

Something like this should work

<%= link_to image_path("portfolio-05-1170x600.jpg"), data: { 'photo-swipe-item' => "", 'size' => '1170x600' } do %>
<%= image_tag("portfolio-05-570x420.jpg", alt: "", :size => "570x420") %>
<% end %>

Best way to use html5 data attributes with rails content_tag helper?

Rails 3.1 ships with built-in helpers:

http://api.rubyonrails.org/classes/ActionView/Helpers/TagHelper.html#method-i-tag

E.g.,

tag("div", :data => {:name => 'Stephen', :city_state => %w(Chicago IL)})
# => <div data-name="Stephen" data-city-state="["Chicago","IL"]" />

How do I create a link_to tag with both class and data fields?

This is what it should look like:

<%= link_to "What's This?", "#", {:class => "more_info", :data => { :more_info => 'mt_hashes_info' }} %>

Class and Data both go into the same options hash.

rails and html data attributes: use dash(-) or underscore(_)?

After tests I did: seems like in development I had gems in version 3.2.9, and in production it was 3.2.12 - the wrong behavior (convert to <a date-last_name/>) was fixed between those versions, and after upgrading my development version I can see the change.

set data-* attribute

You can use content_tag instead of tag to produce a <div></div> instead of a <div>, but you shouldn't.

You're using Slim, so use Slim:

.agile-carouesel data-carousel_list=url_for(format: :json)

Output:

<div class="agile-carouesel" data-carousel_list="some URL"></div>

The whole point of Slim and HAML is that you have this fantastically terse syntax for writing tags. You should use it, rather than resorting to Rails' tag helpers when they're not needed. By using them, you're reintroducing all the punctuation clutter that they're supposed to remove.

Link to an Anchor on page in Rails App

The url helper method can take an anchor hash as a parameter, and will output it as an anchor link:

<li><%= link_to "Ultra", product_path(anchor: 'ultra') %></li>

From the docs:

link_to can also produce links with anchors or query strings:

link_to "Comment wall", profile_path(@profile, anchor: "wall")
# => <a href="/profiles/1#wall">Comment wall</a>


Related Topics



Leave a reply



Submit