Note that there are some explanatory texts on larger screens.

plurals
  1. POcreate comments for nested routes
    primarykey
    data
    text
    <p>I'm trying to make a blog which has users, posts and comments. Each user can have many posts and many comments similarly each post can have many comments. I have successfully made the user and post sections but am having difficulty creating comments and then displaying them.</p> <p>code:</p> <p>routes.rb :</p> <pre><code>resources :users do resources :posts do resources :comments end end </code></pre> <p>user.rb :</p> <pre><code>has_many :posts, dependent: :destroy has_many :comments, dependent: :destroy </code></pre> <p>post.rb :</p> <pre><code>belongs_to :user has_many :comments, dependent: :destroy </code></pre> <p>comment.rb :</p> <pre><code>belongs_to :post, :user </code></pre> <p>I'm creating and displaying comments in the post's view itself so..</p> <p>posts_controller.rb :</p> <pre><code>def show @user = current_user @post = Post.find(params[:id]) end </code></pre> <p>view/posts/show.html.erb :</p> <pre><code>&lt;p&gt;&lt;strong&gt;Title:&lt;/strong&gt;&lt;%= @post.title %&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Text:&lt;/strong&gt;&lt;%= @post.text %&gt;&lt;/p&gt; &lt;% if @user.posts.comments.empty? %&gt; &lt;h2&gt;Comments&lt;/h2&gt; &lt;%= render @posts.comments %&gt; &lt;% end %&gt; &lt;h2&gt;Add a comment:&lt;/h2&gt; &lt;%= render "comments/form" %&gt; &lt;%= link_to 'Edit Post', edit_user_post_path(@user.id,@post) %&gt; | &lt;%= link_to 'Back to Posts', user_posts_path(@user.id) %&gt; </code></pre> <p>comments_controller.rb :</p> <pre><code>class CommentsController &lt; ApplicationController def create @user = current_user @post = @user.posts.find(params[:post_id]) @comment = @user.posts.comments.create(params[:comment]) redirect_to user_post_path(@user.id,@post) end def destroy @user = current_user @post = @user.posts.find(params[:post_id]) @comment = @user.posts.comments.find(params[:id]) @comment.destroy redirect_to user_post_path(@user.id,@post) end end </code></pre> <p>And the partials are:</p> <p>views/comments/_form.html.erb :</p> <pre><code>&lt;%= form_for([@user,@post,@comment]) do |f| %&gt; &lt;p&gt; &lt;%= @user.email %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.label :body %&gt;&lt;br /&gt; &lt;%= f.text_area :body %&gt; &lt;/p&gt; &lt;p&gt; &lt;%= f.submit %&gt; &lt;/p&gt; &lt;% end %&gt; </code></pre> <p>I think my form_for is not right here but I'm new to rails and I've also tried form_for(@user,@post,@post.comments.build) but that didn't work either.. Anyways here's another partial:</p> <p>views/comments/_comment.html.erb:</p> <pre><code>&lt;p&gt;&lt;strong&gt;Commenter:&lt;/strong&gt;&lt;%= @user.email %&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Comment:&lt;/strong&gt;&lt;%= comment.body %&gt;&lt;/p&gt; &lt;p&gt;&lt;%= link_to 'Destroy Comment', [comment.post, comment],method: :delete, data: { confirm: 'Are you sure?' } %&gt; &lt;/p&gt; </code></pre> <p>Again here I'm having trouble with link to...any suggestions would be great.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload