Link posts and comments on a new view/controller

8 views
Skip to first unread message

phi...@bailey.st

unread,
Dec 3, 2013, 11:23:02 AM12/3/13
to rubyonra...@googlegroups.com


Hello everyone,

I'm a newbie following the
http://guides.rubyonrails.org/getting_started.html , so far everyone is
working without problems except a modification that I made earlier.

I've created a view called welcome that act as main content page where I
list all the posts, going a bit further I created a new model called
comments with its own controller. At this point I'm stuck because I'm
unable to include the comments into the welcome/index view.


This is the content of comments_controller.rb


class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment].permit(:commenter,
:body))
redirect_to post_path(@post)
end
end



And this is the welcome/index.html.erb

<h1>My posts</h1>

<table>
<tr>
<th>Title</th>
<th>Text</th>
</tr>

<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %></td>
<td><%= link_to 'View/Post Comments', post %></td>

</tr>
<% end %>
</table>


<p>
<%= link_to "Admin", controller: "posts" %>



Any clue how to include the comments into the index page?


Sorry again for the noob question.

Phillip


0x92E36BFF.asc

phi...@bailey.st

unread,
Dec 3, 2013, 5:04:35 PM12/3/13
to rubyonra...@googlegroups.com

Just sorted out using Nested Eager Loading.




<% post.comments.each do |comment| %>
<td><%= comment.commenter %></td>
<td><%= comment.body %></td>
--
www.bailey.st | www.smoothsec.org
0x92E36BFF.asc
Reply all
Reply to author
Forward
0 new messages