卡住尝试构建嵌套的注释系统Rails 5

时间:2018-09-27 20:42:55

标签: ruby-on-rails ruby

目前不确定该怎么做,我已按照教程进行操作,但当前甚至无法发表评论。如果我从模型中除去所有可评论的关系/提法,它们就可以正常工作。但是我想为嵌套评论系统构建功能,在其中可以对评论进行评论。这是所有相关的文件+错误。

位于评论new.html.erb页面内的新评论表单

 <div class='form-signin col-md-6 offset-md-3'>
        <%= form_with url: post_comments_path, scope: "comment", local:true do |form| %>

           <div class="form-group"> 
            <%= form.label :body %>
            <%= form.text_area :body, :class => "form-control-lg" %>
            </div>
            <div class="form-group">
            <%= form.hidden_field :user_id, :value =>current_user.id %>
            <%= form.hidden_field :post_id, :value => @post.id %>
            <%= form.hidden_field :commentable_id, :value => @comment.id %>
            <%= form.hidden_field :commentable, :value => @comment.body %>
            </div>

            <%= form.submit %>
        <% end %>
</div>

评论模型

class Comment < ApplicationRecord
  belongs_to :post
  belongs_to :user
  belongs_to :commentable, polymorphic: true
  has_many :comments, as: :commentable
  paginates_per 5

end

评论控制器:

def create
    @post = Post.find(params["post_id"])

    user_id = comment_params["comment"]["user_id"]
    body = comment_params["comment"]["body"]

    @comment = Comment.new(post_id: @post.id, user_id: user_id, body: body )

    if @comment.save
      redirect_to post_path(@post), method: :patch, notice: 'Reply successfully created.'
    else
      redirect_to post_path(@post), notice: @comment.errors.messages

    end
  end

 def comment_params
    params.permit(:post_id, comment: [:user_id, :body] )
  end

The error I get when I try to post a comment currently:

服务器日志:

Started GET "/posts/1" for 127.0.0.1 at 2018-09-27 16:33:56 -0400
Processing by PostsController#show as HTML
  Parameters: {"id"=>"1"}
  Post Load (0.2ms)  SELECT  "posts".* FROM "posts" WHERE "posts"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/posts_controller.rb:40
  Rendering posts/show.html.erb within layouts/application
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 143], ["LIMIT", 1]]
  ↳ app/controllers/application_controller.rb:21
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 19], ["LIMIT", 1]]
  ↳ app/views/posts/show.html.erb:45
   (0.4ms)  SELECT COUNT(*) FROM "comments" WHERE "comments"."commentable_id" = $1 AND "comments"."commentable_type" = $2  [["commentable_id", 1], ["commentable_type", "Post"]]
  ↳ app/views/posts/show.html.erb:49
  CACHE User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 19], ["LIMIT", 1]]
  ↳ app/views/posts/show.html.erb:49
  CACHE User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 19], ["LIMIT", 1]]
  ↳ app/views/posts/show.html.erb:54
  Comment Load (0.4ms)  SELECT "comments".* FROM "comments" WHERE "comments"."commentable_id" = $1 AND "comments"."commentable_type" = $2  [["commentable_id", 1], ["commentable_type", "Post"]]
  ↳ app/views/posts/show.html.erb:71
  Rendered posts/show.html.erb within layouts/application (11.8ms)
Completed 200 OK in 49ms (Views: 42.8ms | ActiveRecord: 1.7ms)

就像我之前说过的那样,注释在没有可注释系统的情况下也可以正常工作,但是如果可能,请在我当前的结构中添加此功能。没有为此功能安装任何宝石。

2 个答案:

答案 0 :(得分:1)

尝试添加optional: true

class Comment < ApplicationRecord
 belongs_to :commentable, polymorphic: true, optional: true

在Rails 5中,每当我们定义一个Emirates_to关联时,都需要在此更改后默认显示关联记录。

阅读this

答案 1 :(得分:0)

首先,您在上述代码中有一个安全漏洞:<%= form.hidden_field :user_id, :value =>current_user.id %>不需要这样做。请从会话中获取当前用户,而不是从控制器中获取。使用上面的代码,可以更改视图中的用户ID并提交,以便将注释附加到另一个用户。

我接下来看到的是以下内容:

<%= form.hidden_field :commentable, :value => @comment.body %>

commentable是您的关系,为什么要在此处分配评论的正文?应该是commentable_type,并且值应该是注释的类名,因此:         <%= form.hidden_field :commentable_type, :value => @comment.class.name %>

在您的控制器中,您未分配注释,因此验证失败,因为需要注释。

此外,由于您决定将评论设为多态,因此不再需要引用该帖子。您只需要保留对可评论内容的引用即可。

GoRails应该有一个很好的例子,我真的很喜欢教练(Chris Oliver)。您可以在此处检查它:https://gorails.com/episodes/comments-with-polymorphic-associations

请按照本教程操作,并确保相应地修改代码。 希望能有所帮助。