我想根据if语法只渲染带有数字字符串输入的帖子的视图。
但是我的代码获得了所有帖子。
index.html.erb
<% @fleamarkets.each do |x| %> // @fleamarket == Fleamarket.all
<% if x.number == '1' %>
<%= render 'fleamarkets/card1', :x => x %>
<% elsif x.number == '2' %>
<%= render 'fleamarkets/card2', :x => x %>
<% else %>
<%= render 'fleamarkets/card3', :x => x %>
<% end %>
<% end %>
_card1.html.erb
<h4 class="card-title" style="text-align: center; border-bottom: 1px solid black;"><%= x.title %></h4>
试试这个...另外,我注意到你正在将“数字”与'1'
作为一个字符串进行比较(你有引号)。对于每个if语句,你很可能需要这样做:if x.number == 1
(注意没有引号)
<% @fleamarkets.each do |x| %>
<% if x.number == '1' %>
<%= render partial: 'fleamarkets/card1', locals: {x: x} %>
<% elsif x.number == '2' %>
<%= render partial: 'fleamarkets/card2', locals: {x: x} %>
<% else %>
<%= render partial: 'fleamarkets/card3', locals: {x: x} %>
<% end %>
<% end %>
尝试删除if语句并替换为:
<%= render "fleamarkets/card#{x.number}", x: x %>