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
Best Way to Remove File Extension
Preventing Delayed_Job Background Jobs from Consuming Too Much CPU on a Single Server
Heroku Won't Reset My Database
Install Bundler Using Rvm, Ruby 1.8.7 with Gcc 4.7 on Linux
What Does It Mean Bundle_Disable_Shared_Gems: '1'
Sinatra - How to Get the Server's Domain Name
Coping with "String Contains Null Byte" Sent from Users
Result.Credit_Card_Verification Is Returning Nil Even on Error in Braintree
How to Programmatically Take Snapshot of Crawled Webpages (In Ruby)
User Selected CSS Stylesheet in Rails
User Theme Switching with SASS - Ruby on Rails
How to Create a Dynamic Regular Expression in Ruby
How to Access a Variable Within a Heredoc in Ruby
How Can a Nested Class Access a Method in The Outer Class in Ruby
How Write into CSV File Properly
What's the Best Way to Start a Background Process, That Can Get Accessed Later On