为嵌套路由创建注释

时间:2013-08-17 12:08:32

标签: ruby-on-rails ruby-on-rails-3 comments nested-routes

我正在尝试创建一个包含用户,帖子和评论的博客。每个用户可以有很多帖子和许多评论,每个帖子可以有很多评论。我已经成功地创建了用户并发布了部分,但是我很难创建注释然后显示它们。

代码:

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

我正在帖子的视图中创建和显示评论,所以......

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

部分是:

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

我认为我的form_for不在这里,但我是rails的新手,我也尝试过form_for(@ user,@ post,@ post.comments.build),但这也不起作用..无论如何这里是另一部分:

视图/评论/ _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> 

再次在这里我遇到链接问题......任何建议都会很棒。

2 个答案:

答案 0 :(得分:2)

你想创建一个包含用户,帖子和评论的博客,我看到你在创建博客之前所做的和我之前所做的事情之间存在一些差异。我会告诉你我做了什么(通过编辑你在问题中发布的文件的代码)然后尝试它,如果它适合你:)

1- routes.rb就像这样

resources :users
resources :posts do
  resources :comments
end

2- user.rb很好,不需要修改

3- post.rb也没关系

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(此视图应该可以让您看到帖子和评论以及新评论的框,以及编辑帖子的链接和帖子索引的链接)

<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(不要忘记再次添加destroy方法)

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(只是尝试以简单的方式使它成为第一个)

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

我希望这项工作能够与您合作,尝试并让我知道它是如何与您合作的,我的博客来自the before code for revised episode 229

答案 1 :(得分:2)

我在这里得到的答案是:

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

评论的部分

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