I don't know how it would work inside a controller. But I think it's better to move the method inside a helper anyway and call it from there. methods defined inside a helper are automatically available for your views
you can do this:
def comment(article)
@comment = current_user.comments.where(article_id: article.id)
end
Then your view will look like this:
<% @articles.each do |article| do %>
<% comment(article) %>
....more code....
Like you said, if you move this in the controller, where(article_id: article.id)
will trip you as you don't know which article
the id
is bound to.
EDIT:
if you really want to access the method inside the controller, you can do as this post suggests:
class ArticlesController < ActionController::Base
def comment(article)
@comment = current_user.comments.where(article_id: article.id)
end
helper_method :comment(article)
end
but why go through the trouble when you can easily do this inside a helper
.