Create comments for nested routes

I am trying to create a blog that has users, posts and comments. Each user can have many posts and many comments, so each post can have many comments. I have successfully created user sections and posts, but it's hard for me to create comments and then show them.

the code:

routes.rb:

resources :users do resources :posts do resources :comments end end 

user.rb:

 has_many :posts, dependent: :destroy has_many :comments, dependent: :destroy 

post.rb:

 belongs_to :user has_many :comments, dependent: :destroy 

comment.rb:

 belongs_to :post, :user 

I create and display comments in the message view, therefore ..

posts_controller.rb:

 def show @user = current_user @post = Post.find(params[:id]) end 

view / posts / show.html.erb:

 <p><strong>Title:</strong><%= @post.title %></p> <p><strong>Text:</strong><%= @post.text %></p> <% if @user.posts.comments.empty? %> <h2>Comments</h2> <%= render @posts.comments %> <% end %> <h2>Add a comment:</h2> <%= render "comments/form" %> <%= link_to 'Edit Post', edit_user_post_path(@user.id,@post) %> | <%= link_to 'Back to Posts', user_posts_path(@user.id) %> 

comments_controller.rb:

 class CommentsController < 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 

And partial:

views / comments / _form.html.erb:

 <%= form_for([@user,@post,@comment]) do |f| %> <p> <%= @user.email %> </p> <p> <%= f.label :body %><br /> <%= f.text_area :body %> </p> <p> <%= f.submit %> </p> <% end %> 

I think my form_for is wrong here, but I'm new to rails, and I also tried form_for (@user, @post, @ post.comments.build), but that didn't work either. Anyways here is another partial:

opinions / comments / _comment.html.erb:

 <p><strong>Commenter:</strong><%= @user.email %></p> <p><strong>Comment:</strong><%= comment.body %></p> <p><%= link_to 'Destroy Comment', [comment.post, comment],method: :delete, data: { confirm: 'Are you sure?' } %> </p> 

Here again, I have problems referring to ... any suggestions would be great.

+4
source share
2 answers

I got the answer here:

posts_controller:

 def show @user = current_user @post = @user.posts.find(params[:id]) @comment = @post.comments.new end 

show.html.erb:

 <p><strong>Title:</strong><%= @post.title %></p> <p><strong>Text:</strong><%= @post.text %></p> <% if !@post.comments.empty ? %> <h2>Comments</h2> <%= render @comment %> <% end %> <h2>Add a comment:</h2> <%= render "comments/form" %> <%= link_to 'Edit Post', edit_user_post_path(@user.id,@post) %> | <%= link_to 'Back to Posts', user_posts_path(@user.id) %> 

comments_controller.rb:

 def create @user = current_user @post = @user.posts.find(params[:post_id]) @comment = @post.comments.create(params[:comment]) redirect_to user_post_path(@user.id,@post) end 

partial comment

_form.html.erb:

 <%= form_for([@user,@post,@comment]) do |f| %> <p><%= @user.email %></p> <p><%= f.label :body %><br /> <%= f.text_area :body %> </p> <p><%= f.submit %></p> <% end %> 
+2
source

You want to create a blog that has users, posts and comments, I see some differences between what you did and what I did before when I created the blog. I will tell you what I did (by editing the code of the files published in your question), then try if it works with you :)

1- routes.rb do this:

 resources :users resources :posts do resources :comments end 

2 user.rb does not need modification

3- post.rb is also excellent

4 comments.rb

 belongs_to :post belongs_to :user 

5- posts_controller.rb

  def show @post = Post.find(params[:id]) @comment = Comment.new end 

6- view / posts / show.html.erb (in this view you can see the message and comments and the field for new comments, as well as a link to edit the message and a link to the post index)

 <p><strong>Title:</strong><%= @post.title %></p> <p><strong>Text:</strong><%= @post.text %></p> <h2>Comments</h2> <%= render @posts.comments %> <h2>Add a comment:</h2> <%= render "comments/form" %> <%= link_to 'Edit Post', edit_post_path(@post) %> | <%= link_to 'Back to Posts', posts_path %> 

7- comments_controller.rb (do not forget to add the destruction method again)

 class CommentsController < ApplicationController before_filter :load_post def create @comment = @post.comments.build(params[:comment]) @comment.user_id = current_user.id if @comment.save redirect_to @post, notice: "Added comment." else render :new end end private def load_post @post = Post.find(params[:article_id]) end end 

8- views / comments / _form.html.erb (just try to do it the first easy way)

 <%= form_for([@post,@comment]) do |f| %> <p> <%= f.label :body %><br /> <%= f.text_area :body %> </p> <p> <%= f.submit %> </p> <% end %> 

9- views / comments / _comment.html.erb

 <p><strong>Commenter:</strong><%= comment.user.email %></p> <p><strong>Comment:</strong><%= comment.body %></p> <p><%= link_to 'Destroy Comment', [comment.post, comment],method: :delete, data: { confirm: 'Are you sure?' } %> </p> 

I hope this works with you as it works with me, try and let me know how it works with you, I got my blog from up to code for revised episode 229 .

+2
source

Source: https://habr.com/ru/post/1497573/


All Articles