簡體   English   中英

嵌入紅寶石if語句

[英]embedded ruby if statement

我想評估一個if語句,以確定是否要向此鏈接添加一個類(除了up_vote ):

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

我該怎么做呢?

只需內聯:

<%= link_to "&uArr;".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 "&uArr;".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 "&uArr;".html_safe, video_votes_path( :video_id => video.id, :type => "up" ), :method => :post, :remote => true, :class => class_name %>

在添加另一個類之前,請注意空格。 這就是您在HTML中有多個類的方式

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM