ruby on rails - How do I get this case statement to work? -


in view, doing this:

<% case @post   when @post.has_children? %>     <% @post.children.each |child| %>              <li><%= link_to child.title, post_path(child)%></li>             <% end %> <% when @post.has_siblings? %>     <% @post.siblings.where.not(id: @post.id).each |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 no related posts. </p> <% end %> 

basically want want check @post variety of conditions. if has_children?, if has_siblings?, etc.

i don't want statement exit if of above true or false.

once view loads, should automatically check of these statements. if finds of above true, should execute command right below check.

the issue when this, defaults else. i.e. case statement doesn't work.

i know bunch of disjointed if statements, html around gets bit weird.

is there way can case statement?

edit 1

the reason if statement doesn't work properly, if have 3 if statements back - none of interact each other (that's way cycle through of conditions properly), else doesn't trigger properly.

e.g. if first 2 conditions true, third not...it print out "there no related posts"...when that's not case. case there no parent posts.

basically want have catch-all related posts, iterating through of various options , checking see if relations exist. if do, pulling them out , if don't move on. if none exist, don't print "there no related posts".

the fact view looking looking complex sign may idea refactor logic out of view , place post model belongs. ideally view(s) should end looking this:

<%# 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 no related posts</p> <% end %> 

the paritals:

<%# 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 %> 

then post model can organize logic:

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 automatically, example   end    def other_siblings     self.siblings.where.not(id: self.id)   end    #... end 

i know doesn't directly answer question, imho think it's better solution.


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -