phillip-8CiRN93LU0FeoWH0uzbU5w@public.gmane.org
2013-Dec-03 16:23 UTC
Link posts and comments on a new view/controller
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
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/529E0566.4090805%40bailey.st.
For more options, visit https://groups.google.com/groups/opt_out.
phillip-8CiRN93LU0FeoWH0uzbU5w@public.gmane.org
2013-Dec-03 22:04 UTC
Re: Link posts and comments on a new view/controller
Just sorted out using Nested Eager Loading.
<% post.comments.each do |comment| %>
<td><%= comment.commenter %></td>
<td><%= comment.body %></td>
On 03/12/13 16:23, phillip-8CiRN93LU0FeoWH0uzbU5w@public.gmane.org
wrote:>
>
> 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
>
>
--
www.bailey.st | www.smoothsec.org
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/529E5573.2040200%40bailey.st.
For more options, visit https://groups.google.com/groups/opt_out.