I believe what @benchwarmer wanted to say is that it's better to pass a parameter to _post partial. Straightforward @comments doesn't work, but something like the code below might:
def index
@posts = Post.all
render :partial => @posts, :locals => { :max_comments => 3 }
end
def show
@post = Post.find(params[:id])
render :partial => @post, :locals => { :max_comments => false }
end
In post.html.haml:
= render 'comments/list', :comments => limited_comments(post.comments,max_comments)
Your helper:
def limited_comments(comments,max_comments)
max_comments ? comments.limit(max_comments) : comments
end
I didn't compile, so you may need to further work on parameters that you pass to render :partial (may be you'll have to separately set :partial and :object/:collection in this case, or smth else, I don't remember and didn't try). But I hope, the idea is clear - keep the logical representation (all comments or last 3) separate from processing path (which controller/action). May be you'll later want to display posts with comments embedded in yet another list (last 3 posts for a list of users), then such separation will come handy.
If you don't want to expose all your internal logical details at controller level, you can also do smth like:
def index
@posts = Post.all
render :partial => @posts, :locals => { :comments_list_type => :short }
end
def show
@post = Post.find(params[:id])
render :partial => @post, :locals => { :comments_list_type => :full }
end
Then, in post.html.haml:
= render 'comments/list', :comments => build_comments_list(post.comments,comments_list_type)
Your helper:
def limited_comments(comments,comments_list_type)
case comments_list_type
when :short
comments.limit(3)
when :long
comments.limit(10)
when :full
comments
end
end