【问题标题】:create comments for nested routes为嵌套路由创建注释
【发布时间】:2013-08-19 18:20:31
【问题描述】:

我正在尝试创建一个包含用户、帖子和 cmets 的博客。每个用户可以有很多帖子和很多 cmets,同样每个帖子可以有很多 cmets。我已成功创建用户和帖子部分,但在创建 cmets 并显示它们时遇到了困难。

代码:

routes.rb:

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

用户.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

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

posts_controller.rb:

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

查看/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) %>

cmets_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/cmets/_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.cmets.build) 但这也不起作用..无论如何这里是另一个部分:

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

再次在这里,我无法链接到...任何建议都会很棒。

【问题讨论】:

  • 运行 rake 路由并将输出粘贴到 gist 或 paste.org
  • @rmagnum2002 这是 rake 路由列表link

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


【解决方案1】:

您想创建一个包含用户、帖子和 cmets 的博客,我发现您所做的与我之前创建博客时所做的有所不同。我会告诉你我做了什么(通过编辑你在问题中发布的文件的代码)然后尝试它是否适用于你:)

1- routes.rb 变成这样

resources :users
resources :posts do
  resources :comments
end

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

3-post.rb 也可以

4- cmets.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(这个视图应该让您能够看到帖子和 cmets 以及新 cmets 的框,以及编辑帖子的链接和帖子索引的链接)

<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- cmets_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/cmets/_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/cmets/_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

【讨论】:

  • 问题是当 cmets 嵌套一次时我没有任何问题..当它们是双嵌套时我遇到了问题
  • 当我尝试显示帖子时,它显示 NilClass:Class 的未定义方法 comments' for #&lt;ActiveRecord::Relation:0x61f3338&gt; and if i remove &lt;% if @user.posts.comments.empty? %&gt; &lt;h2&gt;Comments&lt;/h2&gt; &lt;%= render @posts.comments %&gt; &lt;% end %&gt; it shows undefined method model_name'
  • 我需要在用户内部发布帖子,因为一个用户可以有很多帖子,如果一个用户被删除或其他什么,那么它的所有帖子也会被删除:)
  • 还没有尝试过,但我会告诉你,如果它和我一起完成,你试过我的答案吗?
  • 抱歉我没试过。早些时候我做了一些类似于你的代码的东西,其中帖子和 cmets 是嵌套的,用户是分开的,而且效果很好..所以我知道它会起作用,但现在要求改变了,所以我不得不改变代码 :)
【解决方案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) %>

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-09
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    相关资源
    最近更新 更多