我如何让这个案例陈述工作



在我看来,我正在这样做:

<% case @post 
 when @post.has_children? %>
    <% @post.children.each do |child| %> 
            <li><%= link_to child.title, post_path(child)%></li>        
    <% end %>
<% when @post.has_siblings? %>
    <% @post.siblings.where.not(id: @post.id).each do |sibling| %>
            <li><%= link_to sibling.title, post_path(sibling)%></li>                    
    <% end %>
<% when !@post.parent.nil? %>
        <li><%= link_to @post.parent.title, post_path(@post.parent) %></li>
<% else %>
    </ul>
</p>
<p>
    There are no related posts.
</p>
<% end %>

基本上我想做的是我想检查@post的各种条件。如果has_children?,如果has_siblings?,等等。

如果上述任何一项是真的还是假的,我不希望该语句退出。

视图加载后,它应自动检查所有这些语句。如果它发现上述任何 true,它应该在检查正下方执行命令。

问题是当我这样做时,它总是默认为else,即 case 语句不起作用。

我知道我可以简单地做一堆脱节的if语句,但是周围的HTML变得有点奇怪。

有没有办法使用 CASE 语句来做到这一点?

编辑 1

if语句无法正常工作的原因是,如果我有 3 个连续的 if 语句 - 其中没有一个相互交互(这是正确循环所有条件的唯一方法),是else没有正确触发。

例如,如果前两个条件为真,但第三个条件不成立......它将打印出"there are no related posts"...当情况并非如此时。目前没有parent员额。

基本上,我只想有一个包罗万象的related帖子,所以我只是遍历所有各种选项并检查是否存在这些关系。如果他们这样做,我会把他们拉出来,如果他们不这样做,那么他们就会继续前进。如果不存在,那么我不打印"没有相关帖子"。

视图看起来已经很复杂这一事实表明,重构视图中的逻辑并将其放入它所属的 Post 模型中可能是个好主意。理想情况下,视图最终应如下所示:

<%# posts/show.html.erb %>
<% if @post.has_related_posts? %>
   <%= render partial: 'children', collection:  @post.children, as: :child %> 
   <%= render partial: 'siblings', collection:  @post.other_siblings, as: :sibling %> 
   <%= render partial: 'parent', locals:  {parent: @post.parent}%> 
<% else %>
  <p>There are no related posts</p>
<% end %>

帕里塔尔:

<%# posts/_children.html.erb %>
<li><%= link_to child.title, post_path(child)%></li>

<%# posts/_sibling.html.erb %>
<li><%= link_to sibling.title, post_path(sibling)%></li>
<%# posts/_parent.html.erb %>
<% unless parent.nil? %>
  <li><%= link_to parent.title, post_path(parent) %></li>
<% end %>

然后,Post模型可以组织逻辑:

class Post < ActiveRecord::Base
  def has_related_posts?
    !children.to_a.empty? || !other_siblings.to_a.empty? || !parent.nil?
  end
  def children
    self.children || [] # Rails does this automatically, but just for the example
  end
  def other_siblings
    self.siblings.where.not(id: self.id)
  end
  #...
end

我知道这并不能直接回答您的问题,但是恕我直言,我认为这是一个更好的解决方案。

这里有两个选择。

使用 IF ELSIF

<% if @post.has_children? %>
  <% @post.children.each do |child| %> 
    <li><%= link_to child.title, post_path(child)%></li>        
  <% end %>
<% elsif @post.has_siblings? %>
  <% @post.siblings.where.not(id: @post.id).each do |sibling| %>
    <li><%= link_to sibling.title, post_path(sibling)%></li>                    
  <% end %>
<% elsif !@post.parent.nil? %>
  <li><%= link_to @post.parent.title, post_path(@post.parent) %></li>
<% else %>
  </ul>
</p>
<p>
  There are no related posts.
</p>
<% end %>

仅使用提到的 doz 大小

<% case
  when @post.has_children? %>
    <% @post.children.each do |child| %> 
      <li><%= link_to child.title, post_path(child)%></li>        
  <% end %>
  <% when @post.has_siblings? %>
    <% @post.siblings.where.not(id: @post.id).each do |sibling| %>
      <li><%= link_to sibling.title, post_path(sibling)%></li>                    
  <% end %>
  <% when !@post.parent.nil? %>
    <li><%= link_to @post.parent.title, post_path(@post.parent) %></li>
  <% else %>
      </ul>
    </p>
    <p>
      There are no related posts.
    </p>
<% end %>

您可以使用 case 语句来做到这一点。只需不给大小写参数。例如,而不是case @post只使用case

这相当于 if 语句。并且应该为你想要实现的目标工作

查看 ruby case 语句以获取一些示例

最新更新