How do I include title and class attribute in link_to? - html

I'm trying this:
<%= link_to 'Test', '#', {title: "this is my images tooltop message", class: "tooltip"} %>
Which returns the following HTML:
Test
When I remove the class attribute, I then get:
Test
How can I set both the title and class in the anchor tag so I get the following?
Test

You can do also:
<%= link_to 'Test', '#', :class => "tooltip", :title => 'this is my images tooltop message' %>
it's just which hash notation you prefer.

I'm useless! What I'm doing is in fact correct.
The tooltip jQuery plugin I'm using removes the title attribute - http://calebjacob.com/tooltipster/.

<%= link_to 'Show', '#', { :title => "this is my images tooltop message", :class => "tooltip"} %>
The older hash notation renders out the perfect HTML. I don't know why the new hash syntax results in this weird behavior though.

<%= link_to 'Test', '#', title: "this is my images tooltop message", class: "tooltip" %>

Related

how to set an unique id for link_to tag in rails?

I need to separate unique ids for some linking purpose, I just want to know how to make an unique id for link_to?
Like I am using:
<%= link_to "Edit", edit_question_path(question.id),method: :get, :remote => true, :id => #question.id%>
This didn't work then I tried:
<%= link_to "Edit", edit_question_path(question.id),method: :get, :remote => true, id: myBtn_<%=question.id%> %>
Nothing worked. Is there any solution?
Try with:
<%= link_to 'Edit',
edit_question_path(question.id),
method: :get,
remote: true,
id: "myBtn_#{question.id}" %>
You need to see what's the way to access a question variable, as instance or local one (I used a question, as you're doing with the path), and myBtn_<%=question.id%> won't work if you don't have defined a myBtn variable, even less if you try to include erb tags to print inside the erb open-close for the link_to.

Controller and action within link_to image_tag don't execute controller code

My link_to looks like this:
<%= link_to image_tag(user_likes_selection.page_picture, :image_id =>
user_likes_selection.id, :controller => :preferences_controller,
:action => :checked_average_with_profile) %>
My controller, preferences_controller, has a method called checked_average_with_profile, which, as far as I can tell, is not being called when I click the image.
The html code that is generated from the link_to is
<img>
<a href="/preferences"><img action="checked_average_with_profile" alt="Soul_surfer_film"
controller="preferences_controller" height="70%" image_id="3254"
src="/assets/soul_surfer_film.jpg" width="70%" /></a>
</img>
Why isn't the controller code executed when the image is clicked?
in cases like these, it's easier to read the code if you use the block form of link_to
<%= link_to { :image_id => user_likes_selection.id, :controller => :preferences, :action => :checked_average_with_profile } do %>
<%= image_tag(user_likes_selection.page_picture %>
<% end %>
in your routes, you can also pass an as option so you can use a named route. assuming your routes looks like
match '/preferences/checked_average_with_profile/:image_id' => 'preferences#checked_average_with_profile', as: :check_average_profile
you can simplify your link using
link_to image_tag(user_likes_selection.page_picture), check_average_profile_path(user_likes_selection.id)
Here is how i do in my code.
<%=link_to(image_tag(user_likes_selection.page_picture), check_average_profile_path(user_likes_selection.id)) %>
Try:
<%= link_to image_tag(user_likes_selection.page_picture), url_for({:controller => 'preferences_controller', :action => 'checked_average_with_profile', :image_id => user_likes_selection.id}) %>
Put your paren after user_likes_selection.id, not at the end. You're mixing image tag properties with your link_to properties.
Try:
<%= link_to image_tag(user_likes_selection.page_picture, :image_id =>
user_likes_selection.id), {:controller => :preferences,
:action => :checked_average_with_profile} %>
Finally solved my problem by adding a collection with my action in resources:
resources :preferences do
collection do
get 'save_new_scores_to_profile'
get 'checked_average_with_profile'
end
end
Then, I modified my view code so that I could pass the image_id variable along to the controller.
<%= link_to image_tag(user_likes_selection.page_picture,
checked_average_with_profile_preferences_path(:image_id =>
user_likes_selection.id) %>
In my controller, I made sure to grab the image_id with params and put a redirect_to at the end:
def checked_average_with_profile
params[:image_id]
redirect_to preferences_url
end
If you have this problem, the key parts are passing the id (whatever that may be) within parenthesis of the controller path you specify and using a COLLECTION instead of a MEMBER in your routing file.

link_to_unless_current assign class to disabled (current) link

Anyone know if you can assign a tag and class to the disabled or current link? The example below only displays as plain text in the browser for the current link.
I have a bit of rails code displaying a list of buttons for each design in the database.
<% #id_cards.each do |id| %>
<%= link_to_unless_current id.design_type, id_card_design_path(id.id), :class => 'btn' %>
<% end %>
The active links are assigned the correct class and display as buttons.
link_to_unless_current accepts a block which can be used to override the default behavior.
http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to_unless_current
<%=
link_to_unless_current("Comment", { :controller => "comments", :action => "new" }) do
link_to("Go back", { :controller => "posts", :action => "index" })
end
%>
In the example above it would yield the 'Go back' link if the current page was the 'new comment' page.
#James gave proper answer its just you are too young to take it right :)
<% #id_cards.each do |id| %>
<%=
link_to_unless_current(id.design_type, id_card_design_path(id.id), :class => 'btn') do
content_tag(:p, id.design_type, :class => :some_class
end
%>
<% end %>

Adding span tag in Rails link_to

I've looked on SO about how to add a <span> tag but I didn't see an example that placed the <span> where I want using Rails 3 link_to:
<span id="span">My span </span>My data
I tried something like:
<%= link_to(content_tag{:span => "My span ", :id => "span"} #user.profile.my_data, "#", {:class => "button white"}) %>
But that didn't work.
link_to can take a block so I think you're after something like this:
<%= link_to '#', :class => 'button white' do %>
<span id="span">My span </span><%= #user.profile.my_data %>
<% end %>
A combination of the .html_safe with #{#user.profile.my_data} should work as well.
<%= link_to "<span id='span'>My span </span>#{#user.profile.my_data}".html_safe, "#", class: 'button white' %>
You can also use a content_tag so it would look like:
<%= link_to(content_tag(:span, "My span ", id:"span")+"#{#user.profile.my_data}", "#", class: 'button white' %>
They're basically identical but one might be easier on the eyes for you. Also, I'm pretty new to coding so if this is dead wrong for some crazy reason, please just comment and I'll change it. Thanks.
link_to '#', :class => 'button white' do
<span id="span">My span </span>My data
end
In HAML :
= link_to new_post_mobile_path(topic.slug), class: 'add-new-place-btn' do
%span{:class => "glyphicon glyphicon-plus", :style => "margin-right: 4px;"}
New Place

Adding data- attribute to embedded Ruby

I want to add this attribute:
data-show-url="<%= video_path(#video) %>"
to the HTML that this embedded Ruby produces:
<%= link_to 'video', video.video_url, :class => 'oembed' %>
What do I do in the embedded ruby?
<%= link_to 'video', video.video_url, :class => 'oembed', :'data-show-url' => video_path(#video) %>
If it is a parameter you want to add, you can simply pass it like this:
<%= link_to 'video', video.video_url, :class => 'oembed', :data-show-url => video_path(#video) %>
You will get it as params[:data-show-url] and the link will be appended with ?data-show-url=value.