Rails link_to_if Class Doesnt Display Correctly - ruby-on-rails

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.

Related

rails params.merge in link_to

I am trying to add a URL parameter in a link_to block.
The code currently <%= link_to "Submit", :action => 'renderChart', :class => "btn", :remote => true, :params => params.merge(:limit => 5) %>
but this gives me an error.
It adds the :class and :action into the url parameter, not just the :limit. Why?
EDIT:
I add other URL params from another link that looks like this
link_to "Toggle Sort Direction",:action => 'renderChart', :remote => true, :params => {:sort => "desc"}
so when the user clicks the other link I want to add the limit to the url params and keep the sort params
Use this
<%= link_to "Submit",{ :action => 'renderChart', :remote => true, :limit => 5, :sort => "desc"}, :class => "btn" %>
Separate out the html_options: class is an html_option so pass it last.
Refer to link_to documentation.
UPDATE
As per the OP's concern in EDIT section of Question:
I add other URL params from another link that looks like this
link_to "Toggle Sort Direction",:action => 'renderChart', :remote => true, :params => {:sort => "desc"}
params :sort => "desc" are for Toggle Sort Direction link and they cannot be connected to the Submit link. When you click on a particular link, params specified in the link would be added to the params hash. So, if you need to pass :sort => "desc" as params upon clicking on Submit link then specify them explicitly as shown in my answer above.
I finally managed to get a solution myself.
If I very simply do this: :params => {:limit => ..., :sort => params[:sort]} i get exactly what I need. If there is a sort param it keeps it the way it is.
You need to explicitly separate the hashes:
<%= link_to "Submit", { :action => 'renderChart', :class => "btn", :remote => true }, params.merge(:limit => 5) %>
Take the link_to out and you have an implicit hash (key-value pairs) and Ruby is smart enough to know you want a hash:
:action => 'renderChart', :class => "btn", :remote => true, params.merge(:limit => 5)
But that last thing - that's not a key-value pair - it's a hash. So really, you have this:
{ :action => 'renderChart', :class => "btn", :remote => true, { ... } }
If you take Rails out of the mix:
{ x: 'value', {} }
And that's simply not a valid Hash :)

how put a class in a span inside of link?

<%= link_to content_tag(:span, :class => "ava-icon") %>
I'm trying to put in a class of the span on a link. I've tried this method and others without success.
The second argument of content_tag is the contents of the tag. If you just want an empty span tag then:
content_tag :span, nil, :class => 'ava-icon'
# "<span class="ava-icon"></span>"
Maybe you're just passing the option through to the wrong thing:
link_to(content_tag(:span, :class => 'x'))
# => <a><span class="x"></span></a>
link_to(content_tag(:span), :class => 'x')
# => <a class="x"><span></span></a>
Presumably you also want to have a destination for your link.

syntax for link_to with parameters and a class?

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

Disable link_to tag in Rails3+

I used the following code:
<%= link_to image_tag("edit.png", :alt => "Edit"), edit_user_path(user) %>
I want to disable this link and image, so I added :disabled=>true to the code, but it's not disabling. Why not, and how do I disable them?
I'm not sure what #lamrin wanted with this question, but I suppose that it is something like this:
<%= link_to_if condition?, image_tag("edit.png", :alt => "Edit"), edit_user_path(user) %>
With this code above, the image would have a link if the condition? is true
In my case this code below worked (a more complicated example):
link_to_unless disabled, (content_tag :div, "", :class => "vote " + vote_class, :title => title), resource_user_path({ :id => resuser.id, :resource_user => {:id => resuser.id, :resource_id => resource_id, :user_id => current_user_id, :vote => vote_value}}), :remote => true, :method => http_method
This link may also help with this approach:
http://railskey.wordpress.com/2012/07/19/rails-link_to-link_to_if-and-link_to_unless/
Unlike buttons, hyperlinks cannot be "disabled". You can do the following though, assuming you have jQuery included on your pages:
<%=link_to image_tag("edit.png", :alt=>"Edit"), edit_user_path(user), :id => "mylink" %>
Add the following Javascript to your page:
$('#mylink').click(function(e){
e.preventDefault();
});
In answer to your question, there is no :disabled option for the link_to helper in Rails, and it is not a valid attribute for a elements either. I believe the reason people tend to get confused with this in Rails is that ":disabled => true" does work IF you are using Bootstrap. So to fix this issue you can either follow Gupta's approach, or just add Bootstrap (which will give you some default CSS as well, so people don't get frustrated trying to click the link)!
Re: link_to method in rails: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)
Re: the "disabled" attribute on a elements: Is 'disabled' a valid attribute for an anchor tag
Re: Bootstrap "disabled" class or attribute with bootstrap: http://getbootstrap.com/css/#anchor-element-1
1)One solution is to render just image_tag when you do not want link and use link_to when u want link to be click enabled. you can use instance variables to control what to render.
2) or use Javascript as suggested.
Use 2 if you want to dynamically do it.
You may use conditional link_to:
<%=
link_to_if(#current_user.nil?, "Login", { :controller => "sessions", :action => "new" }) do
link_to(#current_user.login, { :controller => "accounts", :action => "show", :id => #current_user })
end
%>

Adding span tags to ruby link?

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 %>

Resources