embedded ruby if statement
I want to evaluate an if statement to determine whether to add a class (in addition to up_vote
) to this link:
<%= link_to "⇑".html_safe, video_votes_path( :video_id => video.id, :type => "up" ), :meth开发者_开发百科od => :post, :remote => true, :class => 'up_arrow' %>
How do I do this?
Just do it inline:
<%= link_to "⇑".html_safe, video_votes_path( :video_id => video.id, :type => "up" ), :method => :post, :remote => true, :class => "up_arrow #{condition ? 'other_class' : ''}" %>
Something like this?
<%
html_class = 'up_arrow'
if some_condition == true
html_class += 'another-class'
end
%>
<%= link_to "⇑".html_safe, video_votes_path( :video_id => video.id, :type => "up" ), :method => :post, :remote => true, :class => html_class %>
But refactoring it into a helper or something wouldn't hurt.
<%
class_name = "up_vote"
if condition
class_name << " another_class"
end
%>
<%= link_to "⇑".html_safe, video_votes_path( :video_id => video.id, :type => "up" ), :method => :post, :remote => true, :class => class_name %>
Note the space before adding another class. This is how you have multiple classes in HTML
精彩评论