当前位置: 动力学知识库 > 问答 > 编程问答 >

What is the correct way to pass multiple arguments to a ruby == operator?

问题描述:

I'm a noob trying to do this:

 <% if @page[:title] == "Portraits" %>

<%= render :partial => "/shared/slideshow" %>

<% elsif @page[:title] == "Escapes" %>

<%= render :partial => "/shared/slideshow" %>

<% elsif @page[:title] == "Articulos pa Web" %>

<%= render :partial => "/shared/slideshow" %>

<% end %>

There has to be a concise way to do this, but I just can't figure it out.

网友答案:

Avoid putting the logic you currently have, in a view.

Put this in a helper method instead and use it in the view:

def get_partial_to_render
  if ["Portraits","Escapes","Articulos pa Web"].include? @page[:title]
    "shared/slideshow"
  else
    "some_other_template"
  end
end
#Note that the partial should not have a leading `/` in the path to it.

And in your view:

<%= render :partial => get_partial_to_render  %>  


Or, if you do not want to render a partial if a name is not in the array:

def render_my_partial?
  ["Portraits","Escapes","Articulos pa Web"].include? @page[:title]
end

<%= render :partial => "shared/slideshow" if render_my_partial? %>  

Note that the ? is part of the method name. Isn't Ruby wonderful? :D

网友答案:
<% if ["Portraits", "Escapes", "Articulos pa Web"].include?(@page[:title])  %>
    <%= render :partial => "/shared/slideshow"  %>
<% end %>
分享给朋友:
您可能感兴趣的文章:
随机阅读: