Ruby on rails 多态注释:can';t通行证类型和id(NoMethodError代表nil:NilClass)

Ruby on rails 多态注释:can';t通行证类型和id(NoMethodError代表nil:NilClass),ruby-on-rails,polymorphic-associations,Ruby On Rails,Polymorphic Associations,我试图为问题和答案创建多态注释,但是当我点击“创建注释”表单上的“创建”按钮时,可注释类型和可注释id从未通过。我可以通过rails控制台成功地创建一个新的注释,但不能通过表单(目前我正在测试注释的问题)。我在控制器的load\u commentable或create中遇到错误,例如: NoMethodError (undefined method `comments' for nil:NilClass): app/controllers/comments_controller.rb:9:in

我试图为
问题
答案
创建多态注释,但是当我点击“创建注释”表单上的“创建”按钮时,
可注释类型
可注释id
从未通过。我可以通过rails控制台成功地创建一个新的注释,但不能通过表单(目前我正在测试注释的问题)。我在控制器的
load\u commentable
create
中遇到错误,例如:

NoMethodError (undefined method `comments' for nil:NilClass):

app/controllers/comments_controller.rb:9:in `create'
似乎
@commentable
nil
(因为它没有
commentable\u type
commentable\u id
参数?)。 我错过了什么

请注意:

class CreateComments < ActiveRecord::Migration[5.1]
  def change
    create_table :comments do |t|
      t.references :user, foreign_key: true
      t.text :body, null:false
      t.references :commentable, polymorphic: true, index: true

      t.timestamps
    end
      add_index :comments, [:commentable_id, :commentable_type]
  end
end
评论表单部分_comment_form.html.slim:

    h4 Add a comment
    .comment_errors
    = form_for [@commentable, Comment.new], remote: true do |f|
      .form-group
        = f.label :body, 'Comment'
        = f.text_area :body, class: 'form-control'
      p= f.submit 'Create', class: 'btn btn-primary'
财务主任评论:

class CommentsController < ApplicationController
  before_action :authenticate_user!
  before_action :load_commentable, only: [:create]

  def create
    @comment = @commentable.comments.create(comment_params.merge(user: current_user))
  end

  private

  def load_commentable
    if params[:comment][:commentable_type] == 'Answer'
      @commentable = Answer.find(params[:comment][:commentable_id])
      gon.answer_id = @commentable.id
    elsif params[:comment][:commentable_type] == 'Question'
      gon.answer_id = @commentable.id
      @commentable = Question.find(params[:comment][:commentable_id])
    end
  end

  def comment_params
    params.require(:comment).permit(:body, :commentable_type, :commentable_id )
  end
end
错误日志:

app/controllers/comments_controller.rb:7:in `create'
Started POST "/comments" for 10.0.2.2 at 2017-11-20 20:54:42 +0000
Cannot render console from 10.0.2.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by CommentsController#create as JS
  Parameters: {"utf8"=>"✓", "comment"=>{"body"=>"kkkk"}, "commit"=>"Create"}
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 41], ["LIMIT", 1]]
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.4ms)



NoMethodError (undefined method `comments' for nil:NilClass):

app/controllers/comments_controller.rb:7:in `create'

我认为您的
partial\u comment\u form.html.slim
应该是这样的:

h4 Add a comment
.comment_errors
= form_for [@commentable, Comment.new], remote: true do |f|
  .form-group
    = f.label :body, 'Comment'
    = f.text_area :body, class: 'form-control'
    = hidden_field_tag 'commentable[id]', @commentable.id
    = hidden_field_tag 'commentable[type]', @commentable.class.name
  p= f.submit 'Create', class: 'btn btn-primary'
Parameters: {"utf8"=>"✓", "comment"=>{"body"=>"kkkk"}, "commentable"=>{"id"=>"1", "type"=>"Question"}, commit"=>"Create"}
def load_commentable
  @commentable = commentable_params[:type].constantize.find_by(id: commentable_params[:id])
  gon.answer_id = @commentable.id
end
def commentable_params
  param.require(:commentable).permit(:id, :type)
end
这将为您提供类似以下内容的参数:

h4 Add a comment
.comment_errors
= form_for [@commentable, Comment.new], remote: true do |f|
  .form-group
    = f.label :body, 'Comment'
    = f.text_area :body, class: 'form-control'
    = hidden_field_tag 'commentable[id]', @commentable.id
    = hidden_field_tag 'commentable[type]', @commentable.class.name
  p= f.submit 'Create', class: 'btn btn-primary'
Parameters: {"utf8"=>"✓", "comment"=>{"body"=>"kkkk"}, "commentable"=>{"id"=>"1", "type"=>"Question"}, commit"=>"Create"}
def load_commentable
  @commentable = commentable_params[:type].constantize.find_by(id: commentable_params[:id])
  gon.answer_id = @commentable.id
end
def commentable_params
  param.require(:commentable).permit(:id, :type)
end
然后,将
load\u commentable
更改为如下内容:

h4 Add a comment
.comment_errors
= form_for [@commentable, Comment.new], remote: true do |f|
  .form-group
    = f.label :body, 'Comment'
    = f.text_area :body, class: 'form-control'
    = hidden_field_tag 'commentable[id]', @commentable.id
    = hidden_field_tag 'commentable[type]', @commentable.class.name
  p= f.submit 'Create', class: 'btn btn-primary'
Parameters: {"utf8"=>"✓", "comment"=>{"body"=>"kkkk"}, "commentable"=>{"id"=>"1", "type"=>"Question"}, commit"=>"Create"}
def load_commentable
  @commentable = commentable_params[:type].constantize.find_by(id: commentable_params[:id])
  gon.answer_id = @commentable.id
end
def commentable_params
  param.require(:commentable).permit(:id, :type)
end
(顺便说一句,gon.当你有问题的时候,回答这个问题似乎很奇怪。但是,我不知道…)

当然,这需要类似于:

h4 Add a comment
.comment_errors
= form_for [@commentable, Comment.new], remote: true do |f|
  .form-group
    = f.label :body, 'Comment'
    = f.text_area :body, class: 'form-control'
    = hidden_field_tag 'commentable[id]', @commentable.id
    = hidden_field_tag 'commentable[type]', @commentable.class.name
  p= f.submit 'Create', class: 'btn btn-primary'
Parameters: {"utf8"=>"✓", "comment"=>{"body"=>"kkkk"}, "commentable"=>{"id"=>"1", "type"=>"Question"}, commit"=>"Create"}
def load_commentable
  @commentable = commentable_params[:type].constantize.find_by(id: commentable_params[:id])
  gon.answer_id = @commentable.id
end
def commentable_params
  param.require(:commentable).permit(:id, :type)
end

查看您的
参数
“comment”=>{“body”=>“kkkk”}
。看到少了什么吗?然后看看你的表格。你有
@commentable
,但是你在哪里向表单中添加它的值呢?id、用户id、commentable类型和commentable id参数都丢失了,但是我不明白为什么以及如何修复这个Google for
隐藏字段的问题。。。顺便说一句,我不知道id是指什么。但是我想你不需要
user\u id
,因为你可以通过designe使用它(我想类似于
current\u user.id
。但是,我不使用designe,所以这可能不完全正确)。id是注释的id。我以前尝试过隐藏字段,但这不起作用,并且出现了类似的错误,我还了解到更好的解决方案是在控制器中动态传递这些字段,但无法修复代码。