Justin Meltzer
Justin Meltzer

Reputation: 13558

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 "&uArr;".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

Answers (3)

mnelson
mnelson

Reputation: 3012

Just do it inline:

<%= link_to "&uArr;".html_safe, video_votes_path( :video_id => video.id, :type => "up" ), :method => :post, :remote => true, :class => "up_arrow #{condition ? 'other_class' : ''}" %>

Upvotes: 1

Mike Lewis
Mike Lewis

Reputation: 64167

<% 
class_name = "up_vote"
 if condition
   class_name << " another_class" 
 end
%>


<%= link_to "&uArr;".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

harald
harald

Reputation: 6126

Something like this?

<%
html_class = 'up_arrow'
if some_condition == true
  html_class += 'another-class'
end
%>
<%=  link_to "&uArr;".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

Related Questions