我是一个试图这样做的菜鸟:
<% 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 %>
必须有一种简洁的方法来做到这一点,但我无法弄明白。
答案 0 :(得分:4)
避免在视图中放置您当前拥有的逻辑。
将它放在辅助方法中,并在视图中使用它:
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.
在你看来:
<%= render :partial => get_partial_to_render %>
或者,如果您不希望在数组中没有名称时呈现部分:
def render_my_partial?
["Portraits","Escapes","Articulos pa Web"].include? @page[:title]
end
<%= render :partial => "shared/slideshow" if render_my_partial? %>
请注意,?
是方法名称的一部分。 Ruby非精彩吗? :d
答案 1 :(得分:2)
<% if ["Portraits", "Escapes", "Articulos pa Web"].include?(@page[:title]) %>
<%= render :partial => "/shared/slideshow" %>
<% end %>