I have an app with comments and comments. In my action / index view, I repeat the message and display it from the most recent created to the old created at. My comments are embedded in my posts, so in my view / view I want to iterate, although the comments also get them to show from the recently created to the oldest created on. It seems I can make this work if I create a method in my post.rb file. I have it:
post.rb:
def newest_comments self.comments.order("created_at DESC") end
In my view of the post view, I can iterate over the comments for the post, and it works fine:
<% @post.newest_comments.each do |comment| <% end %>
BUT I want to install this functionality at the controller level, but I cannot figure out how to do this. This is what I have in the show action in the message controller:
def index @posts = Post.all.order("created_at DESC") end def show @comment = Comment.new @comments = Comment.all.order("created_at DESC") end
And now my updated post to show:
<% @post.comments.each do |comment| %> <% end %>
@post var exists because of my action before the action. So my question is: why don't I have access to this ivar in my view after the show, just calling it in my view?
source share