Creartor必须存在错误Ruby on Rails

时间:2017-11-08 17:21:07

标签: ruby-on-rails ruby-on-rails-3 associations cloud9

我有两个模型,问题和用户

用户:

class User < ApplicationRecord
    has_many :created_issues, :class_name => 'Issue', :foreign_key => 'creator_id'
    has_many :assigned_issues, :class_name => 'Issue', :foreign_key => 'assigned_id'
end

问题:

class Issue < ApplicationRecord
    belongs_to     :creator,
                   :class_name => "User",
                   :foreign_key  => "creator_id"

    belongs_to     :assigned,
                   :class_name => "User",
                   :foreign_key  => "assigned_id" 
end

迁移文件:

class CreateIssues < ActiveRecord::Migration[5.1]
  def change
    create_table :issues do |t|
      t.string :title
      t.text :description
      t.integer :assigned_id
      t.string :tipo
      t.string :prioridad
      t.string :estado
      t.references :creator
      t.references :assigned

     add_foreign_key :issues, :users, column: :creator_id, primary_key: :id
     add_foreign_key :issues, :users, column: :assigned_id, primary_key: :id
      t.timestamps
    end
  end
end

架构的一部分:

ActiveRecord::Schema.define(version: 20171030104901) do

create_table "issues", force: :cascade do |t|
    t.string "title"
    t.text "description"
    t.integer "assigned_id"
    t.string "tipo"
    t.string "prioridad"
    t.string "estado"
    t.integer "creator_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false`enter code here`
    t.index ["assigned_id"], name: "index_issues_on_assigned_id"
    t.index ["creator_id"], name: "index_issues_on_creator_id"
  end

在我得到的形式中,他的场创造者必须存在,但我在插入中看到它很好:

  

参数:{"utf8"=>"✓", "authenticity_token"=>"2yY/0x6961iIb9PxcyQAKHUSRqEj+zwQ91uPwHibU9tWjEXqiKMqp3vwzI70FVI2agtYhPgljFPDZjVZV4cmyg==", "issue"=>{"title"=>"asdsad", "description"=>"asdsa", "creator_id"=>"2", "tipo"=>"adas", "prioridad"=>"dsdaasd", "assigned_id"=>"3"}, "commit"=>"Create Issue"}
  未经许可的参数:: creator_id
    (0.1ms)开始交易
   用户负载(0.3ms)SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
    (0.1ms)回滚事务

任何帮助?

2 个答案:

答案 0 :(得分:1)

您的creator_id未列入白名单:

Unpermitted parameter: :creator_id

您应该将控制器代码发布到您正在执行的位置params.require(...).permit(...),以便我们了解其中的内容。

它应该类似于:

def issue_params
  params.
    require(:issue).
    permit(:title, :description, :creator_id, :tipo, :prioridad, :assigned_id)
end

答案 1 :(得分:1)

如果你正在使用strong_parameters宝石,那么jvillian的答案就可以了。如果不是(完全可能,因为它在Rails 4之前没有被广泛使用),您可能需要将attr_accessible :creator_id添加到您的问题模型中。