2011-03-20 77 views
0

我要評估一個if語句,以確定是否添加一個類(除up_vote)此鏈接:嵌入式紅寶石if語句

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

我該怎麼辦呢?

回答

1

有多個類只要做到這一點在線:

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

是這樣的?

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

但是將它重構成助手或其他東西不會傷害。

0
<% 
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 %> 

注意添加其他類之前的空間。這是你如何在HTML