how put a class in a span inside of link? - ruby-on-rails

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

Related

Custom font-awesome button with content_tag

I would like to have a button that looks like Log in with [FB], wher [FB] is font-awesome icon. (Note that this icon appears at the end). For this purpose, this works:
= form_tag my_path, :method => :post do
= button_tag do
Log in with
%i.icon-facebook
I would like to DRY this up by create a new helper method:
def button_to_with_icon(path, text, button_class, icon)
form_tag path, :method => :post do
button_tag(:class => button_class) do
text
content_tag :i, "" , :class => icon.to_sym
end
end
end
However, the text argument does not render in HTML. How can I fix this issue?
The button_tag block will use what's returned there as text. Here you are implicitely returning the content_tag, and throwing the text.
You should use concat inside your content_tag block:
button_tag do
concat text
concat content_tag(:i, nil, :class => icon.to_sym)
end
In your code the return value of text simply gets wasted. You have to return a concatenation of both, the content_tag and text:
def button_to_with_icon(path, text, button_class, icon)
form_tag path, :method => :post do
button_tag(:class => button_class) do
text + content_tag(:i, "" , :class => icon.to_sym)
end
end
end
A ruby method is not ERB :-)

Rails link_to_if Class Doesnt Display Correctly

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.

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

adding a class to a link_to is breaking the link

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 to display several image_tag inside an iteration in a helper

I'm trying to create a helper to display rating stars based on a model attribute.
I have the following:
def show_profile_stars(profile)
content_tag :span, :class => 'stars' do
profile.stars.times do
image_tag("stars.gif", :size => "30x30", :class => "gold")
end
end
end
'stars' is an integer field.
But it's not rendering the image tags, instead it just displays the 'stars' number literally.
If I put just image_tag without the iteration block it does show the image, the problem is with the iteration.
I guess I'm missing something about methods that receive blocks (I'm still new at RoR).
Any help?
Thanks!
Use the concat helper:
def show_profile_stars(profile)
content_tag :span, :class => 'stars' do
profile.stars.times do
concat(image_tag("stars.gif", :size => "30x30", :class => "gold"))
end
nil
end
end
You also need to return nil at the end of the content_tag so that it doesn't output stars.
Two things, wouldn't this be done in CSS using a class on the span (e.g. one-star, two-star, etc)?
Anyway, to actually do what you want to do try:
stars = []
profile.stars.times { stars << image_tag("stars.gif", :size => "30x30", :class => "gold") }
content_tag :span, stars.join("\n").html_safe, :class => "stars"

Resources