Rails 2.3.5
For a "link_to" tag, I'm trying to pin down the syntax for sending extra parameters and specifying a class. I'm using the jQuery UI library to change links into buttons with a class of 'link_button'.
This sends the extra 'min_max' parameter, but the class will not be applied:
<%= link_to "CLICK HERE", :action => 'edit', :id => #threshold_control.id, :min_max => 'different', :class => 'link_button' %>
This is not sending the extra 'min_max' parameter, but the 'link_button' class is applied:
<%= link_to 'CLICK HERE',edit_threshold_control_path(#threshold_control.id), :min_max => 'different', :class => 'link_button' %>
I haven't seen a specific example of extra link_to parameters AND a class specified, and none of my guesses at the syntax needed for both things to work at the same time have worked. Thanks for any help.
Try:
<%= link_to "CLICK HERE", { :action => 'edit', :id => #threshold_control.id, :min_max => 'different' }, { :class => 'link_button' } %>
link_to expects two hashes after the name of the link. If you don't use curly braces there is no way to know when the first hash ends and the second hash starts.
If anyone needs to put html inside the link text as I did, here's the alternative.
<%= link_to(options = { :action => 'edit', :id => #threshold_control.id, :min_max => 'different' }, html_options = { :class => 'link_button' }) do %>
HTML HERE
<%end%>
link to documentation here
Related
I'm following this SE question to put a form in a bootstrap modal with rails.
The person who answered the question states: "Make sure that you have a unique id or class for every modal body.". So I am trying to put a unique id number in my link_to:
<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "<%= post.id %>-my-modal" %>
But this is causing an error. If I take out <%= post.id %> I do not have an error, but the modal behavior does not work.
How can I add the post.id with embedded ruby to the link to?
You have to write it like this:
<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#{post.id}-my-modal" %>
Once you open a <% ... %> tag, you are writing ruby code. What this means is that you can't nest <% ... %> tags inside another <% ... %> tag because these tags aren't ruby syntax.
Inside the tag, to do string interpolation, use normal ruby methods:
<%= link_to "Edit", edit_post_path(post.id), :class => "btn", :remote => true, "data-toggle" => "modal", "data-target" => "#{post.id}-my-modal" %>
Only mistake is putting another (ruby) tag <% %> inside the same tag.
In erb (embedded ruby) one <%= %> tag cannot have another tag inside.
This code result a syntax error.
<%= link_to "Edit", .... "data-target" => "<%= post.id %>-my-modal" %>
If you plan to output/render data inside the tag and quoted as string,
used #{put_data_hare}
Output like:
<%= link_to "Edit", .... "data-target" => "#{post.id}-my-modal" %>
In other case, you can do like:
post.id.to_s + "-my-modal"
For the life of me, i cant figure out why this problem is happening. I used the link_to helper all the time, but iv only used the link_to_if helper a few times, and this time I cant get the link to take a CSS class.
Here's my link
<%= link_to_if(step.sequence > 1, raw('<i class="icon-chevron-up"></i>'), url_for(:controller => :test_steps, :action => :update_sequence, :direction => 'up', :id => step.id, :test_script_id => #test_script), { :class => 'btn btn-mini' })%>
The image displays, with no link as expected, but the CSS class defined at the end does not, instead it just has no class. This is the same format I use in all my link_to helpers.
Can anyone explain why?
Using link_to_if, only the name --in your case the result of raw('<i class="icon-chevron-up"></i>')-- will be returned if the condition fails. All options that would otherwise apply to the link tag will be ignored.
See the source.
Try passing an empty hash as the options argument, so that { :class => '...' } is assigned to the html_options argument. Untested
<%= link_to_if(step.sequence > 1, raw('<i class="icon-chevron-up"></i>'), url_for(:controller => :test_steps, :action => :update_sequence, :direction => 'up', :id => step.id, :test_script_id => #test_script), {}, { :class => 'btn btn-mini' })%>
Wrap the link_to_if or link_to_unless in a span:
%span.pull-right
= link_to_unless Foo.deleted.empty?, "<i class='icon-white icon-trash'></i> Undelete Foo".html_safe, deleted_foos_path
Above code sets a css class (pull-right) on whatever is displayed - full link or just its text.
I'm using link_to in RoR 3
When I use it like this, it works fine:
<%= link_to "Add to your favorites list",:controller =>
'favourite_companies', :action =>'create',
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}" %>
But I would like to pass in a class as well
however, this is not working for me. The class works, but it breaks the link. Any ideas?
<%= link_to "Add to your favorites list",{:controller =>
'favourite_companies', :action =>'create'},
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
:class=>"ui-button-text button_text"} %>
<%= link_to "Add to your favorites list",{:controller =>
'favourite_companies', :action =>'create'},
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
:class=>"ui-button-text button_text"} %>
try this
<%= link_to "Add to your favorites list", :controller =>
'favourite_companies', :action =>'create',
:company_id=>"#{#company.id}",
:company_name=>"#{#company.company_name}",
{ :class=>"ui-button-text button_text" } %>
Since the :class should be in :html_options (refering to API)
link_to(body, url, html_options = {})
The proper way of doing what you have is as follows:
link_to "Foo", { URL_FOR PARAMS HERE }, :class => "bar"
As far as setting the controller and action manually like this, well, it's crap. Rails builds url helpers for you; use them and save yourself some time, energy, and add clarity, all at once:
link_to "Foo", favourite_companies_path(#company), :method => :post
What you're doing with the string interpolation is a bad idea too…it's just wasteful and cluttered for no reason at all. The following is the same, just better:
link_to "Foo", :company_id => #company.id, :company_name => #company.name
As far as why your link wasn't working, if wrapping it in a div helped it sounds like you have a problem with your HTML structure, not the link_to syntax.
I'm using a link_to do-end block so the above previous solutions didn't work for me.
If you want to embed other tags in your a tag, then you can use the link_to do-end block.
<%= link_to favourite_companies_path(:company_id => #company.id, :another_url_param_here => "bar"), { :class => "ui-button-text button_text", :title=> "We can have more html attributes as well" } do %>
<i class="fa fa-star"></i>
<%= #company.company_name %>
<% end %>
In this case it's
<%= link_to path(url_params), html_options = {} do %>
<% end %>
Be careful because in Rails 5 the above methods will still result in a wrong URL generation. The controller and action need to be put in a literal hash in order for it to work in Rails 5. What you will have should be something like this
<%= link_to "Add to your favorites list",
{ controller: "favourite_companies", action:"create"},
company_id: #company.id,
company_name: #company.company_name,
class: "ui-button-text button_text" %>
How do you add span tags to the link below in ruby?
<%= link_to (l(:button_show), {:action => 'show', :path => to_path_param(#path)}, :class => "button") %>
I would like to add the span tags to the link like this:
<span>Show</span>
Kevin's answer will work fine. I far prefer his second option (using the block) to putting an interpolated string in as an argument. I might actually prefer this:
<%= link_to content_tag(:span, l(:button_show)), {:action => 'show', :path => to_path_param(#path)}, :class => "button" -%>
content_tag simply returns an HTML string. Its first argument is the name of the tag. The second is the tag content. (There's an alternative block usage, but that would just complicate things here.)
Totally possible. The first parameter to link_to is just an arbitrary string, so you could do this:
<%= link_to("<span>#{l(:button_show)}</span>",
{:action => 'show', :path => to_path_param(#path)}, :class => "button") %>
Since link_to can take a block for the name, though, a more readable way might be to do this:
<% link_to({:action => 'show', :path => to_path_param(#path)}, :class => "button") do %>
<span><%= l(:button_show) %></span>
<% end %>
I am using link_to img tag like following
<%= link_to image_tag("Search.png", :border=>0, :class => 'dock-item'),
:action => 'search', :controller => 'pages'%><span>Search</span></a>
Which results in following html
<a href="/pages/search"><img alt="Search" border="0" class="dock-item"
src="/images/Search.png?1264132800" /></a><span>Search</span></a>
I want the class="dock-item" to go to the <a> tag instead of the img tag.
How can i change this?
Update:
<%= link_to image_tag("Search.png", :border=>0), :action => 'search',
:controller => 'pages', :class => 'dock-item' %>
results in
<a href="/pages/search?class=dock-item"><img alt="Search" border="0"
src="/images/Search.png?1264132800" /></a>
hi you can try doing this
link_to image_tag("Search.png", border: 0), {action: 'search', controller: 'pages'}, {class: 'dock-item'}
or even
link_to image_tag("Search.png", border: 0), {action: 'search', controller: 'pages'}, class: 'dock-item'
note that the position of the curly braces is very important, because if you miss them out, rails will assume they form a single hash parameters (read more about this here)
and according to the api for link_to:
link_to(name, options = {}, html_options = nil)
the first parameter is the string to be shown (or it can be an image_tag as well)
the second is the parameter for the url of the link
the last item is the optional parameter for declaring the html tag, e.g. class, onchange, etc.
hope it helps! =)
Just adding that you can pass the link_to method a block:
<%= link_to href: 'http://www.example.com/' do %>
<%= image_tag 'happyface.png', width: 136, height: 67, alt: 'a face that is unnervingly happy'%>
<% end %>
results in:
<a href="/?href=http%3A%2F%2Fhttp://www.example.com/k%2F">
<img alt="a face that is unnervingly happy" height="67" src="/assets/happyface.png" width="136">
</a>
This has been a life saver when the designer has given me complex links with fancy css3 roll-over effects.
Best will be:
link_to image_tag("Search.png", :border => 0, :alt => '', :title => ''), pages_search_path, :class => 'dock-item'
this is my solution:
<%= link_to root_path do %>
<%= image_tag "image.jpg", class: "some class here" %>
<% end %>
Easy:
<%= link_to image_tag("Search.png", :border=>0), :action => 'search', :controller => 'pages', :class => 'dock-item' %>
The first param of link_to is the text/html to link (inside the a tag). The next set of parameters is the url properties and the link attributes themselves.
I tried this too, and works very well:
<%= link_to home_index_path do %>
<div class='logo-container'>
<div class='logo'>
<%= image_tag('bar.ico') %>
</div>
<div class='brand' style='font-size: large;'>
.BAR
</div>
</div>
<% end %>
To respond to your updated question, according to http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html...
Be careful when using the older argument style, as an extra literal hash is needed:
link_to "Articles", { :controller => "articles" }, :id => "news", :class => "article"
# => Articles
Leaving the hash off gives the wrong link:
link_to "WRONG!", :controller => "articles", :id => "news", :class => "article"
# => WRONG!
The whole :action =>, :controller => bit that I've seen around a lot didn't work for me.
Spent hours digging and this method definitely worked for me in a loop.
<%=link_to( image_tag(participant.user.profile_pic.url(:small)), user_path(participant.user), :class=>"work") %>
Ruby on Rails using link_to with image_tag
Also, I'm using Rails 4.
Hey guys this is a good way of link w/ image and has lot of props in case you want to css attribute for example replace "alt" or "title" etc.....also including a logical restriction (?)
<%= link_to image_tag("#{request.ssl? ? #image_domain_secure : #image_domain}/images/linkImage.png", {:alt=>"Alt title", :title=>"Link title"}) , "http://www.site.com"%>
Hope this helps!
<%= link_to root_path do %><%= image_tag("Search.png",:alt=>'Vivek',:title=>'Vivek',:class=>'dock-item')%><%= content_tag(:span, "Search").html_safe%><% end %>
You can also try this
<li><%= link_to "", application_welcome_path, class: "navbar-brand metas-logo" %></li>
Where "metas-logo" is a css class with a background image