问题描述
|
我是一个菜鸟,尝试这样做:
<% 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 %>
必须有一种简洁的方法来执行此操作,但是我无法弄清楚。
解决方法
避免将您当前拥有的逻辑放在视图中。
将其放在辅助方法中,并在视图中使用它:
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\",\"Articulos pa Web\"].include? @page[:title]
end
<%= render :partial => \"shared/slideshow\" if render_my_partial? %>
注意,“ 4”是方法名称的一部分。露比不好吗:D
, <% if [\"Portraits\",\"Articulos pa Web\"].include?(@page[:title]) %>
<%= render :partial => \"/shared/slideshow\" %>
<% end %>