我有一个投票应用,用户可以投票。每个民意调查都有条件允许用户投票或不投票。
现在我正在做:
<% if current_user.voted_for?(@poll) %>
<p>You already voted this poll</p>
<% elsif @poll.open_date > Date.today %>
<p>Poll is not opened yet</p>
<% elsif @poll.close_date < Date.today %>
<p>Poll is already closed</p>
<% elsif current_user.polls.exists?(@poll.id) %>
<p>You are not allowed to vote on this poll</p>
<% elsif current_user.chair_role? %>
<p>BOD chair role is not allowed to vote</p>
<% else %>
<%= submit_tag 'Vote', class: 'btn btn-lg btn-primary' %>
<% end %>
第一个条件遇到返回相应的文本,不会检查下一个条件。
如何在输出文本中列出满足所有条件?
例如
Poll is already closed
You already voted this poll
我该怎么做?
答案 0 :(得分:4)
您可以使用字符串附加文字,并且您需要多个单独的if
语句。
<%
output = ""
output << "You already voted in this poll. " if current_user.voted_for?(@poll)
output << "Poll is not open. " if @poll.open_date > Date.today
%>
<% if output.blank? %>
<%= submit_tag 'Vote', class: 'btn btn-lg btn-primary' %>
<% else %>
<%= output %>
<% end %>