Domanda

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.

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'm creating and displaying comments in the post's view itself so..

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 the partials are:

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 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:

views/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> 

Again here I'm having trouble with link to...any suggestions would be great.

È stato utile?

Soluzione 2

I got the answer here it is:

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

comment's partial

_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 %>

Altri suggerimenti

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

1- routes.rb make it like this

resources :users
resources :posts do
  resources :comments
end

2- user.rb is fine no need to be modified

3- post.rb also is fine

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 ( this view should make you able to see the post and the comments and a box for the new comments, and a link to edit post and a link to the posts 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 ( don't forget to add the destroy 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 make it first in a simple 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 work with you as it works with me, try it and let me know how it goes with you, i got my blog work from the before code for revised episode 229.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top