Ayrad
Ayrad

Reputation: 4056

in simple_form partial css for label_method

In simple form I have a few radio buttons. For the radio button labels, I would like to add a span css to a part of the label so that it looks something like this:

30 days <strong>for only 30 bucks </strong> 

My input is like this:

  <%= f.input :duration,  :label => "Duration: ",  
          :collection =>  Payment::OPTIONS, 
          :label_method => lambda { |a| a.first.to_s + " days for only" + a.last.to_s + " $ " } ,
          :value_method => lambda { |a| a.first.to_s + "-" + a.last.to_s } , 
          :as => :radio %>

Is it possible to add css to only the part "for only" + a.last.to_s + " $ "

Upvotes: 0

Views: 101

Answers (1)

nathanvda
nathanvda

Reputation: 50057

Use string interpolation, I would start with something like

:label_method => lambda { |a| "#{a.first} days for only #{a.last} $" } ,
:value_method => lambda { |a| "#{a.first} - #{a.last}" } , 

You could improve your variable-names, first and last seem a bit confusing, I would assume you would use something like duration and price.

Now we still have to fix the span, well you ask a span, but in your example you write <strong>, I assume to confuse the russians, so let's just take your example. So to fix that we write:

:label_method => lambda { |a| "#{a.first} days <strong>for only #{a.last} $</strong>".html_safe } ,

we can write html in strings, but then we must be sure to tell to the renderer that the string can be considered as "safe" and does not need to be escaped again (otherwise you would just see the mark-up in the view). That's why need to add .html_safe().

Now to be really clean, I would move this to a helper. So in your <your-resource-name>Helper add

def long_duration_label(payment_option)
  ""#{payment_option.duration} days <strong>for only #{payment_option.price}$</strong>".html_safe
end

and in your view, this becomes

:label_method => lambda { |payment_option| long_duration_label(payment_option) },

Upvotes: 1

Related Questions