在Post模型中,我有一个像“content_type”这样的属性。不同类型的帖子应以不同的方式显示在同一列表中。现在我只有一个想法:
<%=
@posts.each do |d|
if d.content_type == "NormalStory"
render :partial => 'posts/normal', :locals => { :content => d }
elsif d.content_type == "FotoStory"
render :partial => 'posts/foto', :locals => { :content => d}
elsif d.content_type "VideoStory"
render :partial => 'posts/video', :locals => { :content => d }
end
end
%>
请你推荐一些更“优雅”的东西吗?
答案 0 :(得分:4)
我创建了一个帮助器来调整content_type
的部分,如:
def render_post(post)
template = post.content_type.sub(/Story$/, '').downcase
render :partial => "posts/#{template}", :locals => { :content => post }
end
答案 1 :(得分:2)
<% @posts.each do |d| %>
<%= render :partial => get_path(d.content_type), :locals => { :content => d } %>
<% end %>
在帮助程序(app / helpers /)中,您应该定义此辅助方法
def get_path(content_type)
case content_type
when "NormalStory"
'posts/normal'
when "FotoStory"
'posts/foto'
when "VideoStory"
'posts/video'
end
end
答案 2 :(得分:1)
<%=
@posts.each do |d|
if d.content_type == "NormalStory"
view_name = "normal"
elsif d.content_type == "FotoStory"
view_name = "foto"
elsif d.content_type "VideoStory"
view_name = "video"
end
render :partial => "posts/"+view_name, :locals => { :content => d }
end
%>