Reputation: 13558
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" ), :method => :post, :remote => true, :class => 'up_arrow' %>
How do I do this?
Upvotes: 0
Views: 2081
Reputation: 3012
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' : ''}" %>
Upvotes: 1
Reputation: 64167
<%
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
Upvotes: 0
Reputation: 6126
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.
Upvotes: 0