我要評估一個if語句,以確定是否添加一個類(除up_vote
)此鏈接:嵌入式紅寶石if語句
<%= link_to "⇑".html_safe, video_votes_path(:video_id => video.id, :type => "up"), :method => :post, :remote => true, :class => 'up_arrow' %>
我該怎麼辦呢?
我要評估一個if語句,以確定是否添加一個類(除up_vote
)此鏈接:嵌入式紅寶石if語句
<%= link_to "⇑".html_safe, video_votes_path(:video_id => video.id, :type => "up"), :method => :post, :remote => true, :class => 'up_arrow' %>
我該怎麼辦呢?
有多個類只要做到這一點在線:
<%= link_to "⇑".html_safe, video_votes_path(:video_id => video.id, :type => "up"), :method => :post, :remote => true, :class => "up_arrow #{condition ? 'other_class' : ''}" %>
是這樣的?
<%
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 %>
但是將它重構成助手或其他東西不會傷害。
<%
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 %>
注意添加其他類之前的空間。這是你如何在HTML