我想评估一个if语句,以确定是否在此链接中添加一个类(除up_vote
之外):
<%= link_to "⇑".html_safe, video_votes_path( :video_id => video.id, :type => "up" ), :method => :post, :remote => true, :class => 'up_arrow' %>
我该怎么做?
答案 0 :(得分:1)
直接进行内联:
<%= link_to "⇑".html_safe, video_votes_path( :video_id => video.id, :type => "up" ), :method => :post, :remote => true, :class => "up_arrow #{condition ? 'other_class' : ''}" %>
答案 1 :(得分:0)
这样的东西?
<%
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 %>
但是将它重构为帮助者或其他东西都不会受到伤害。
答案 2 :(得分:0)
<%
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中有多个类的方法