创建者必须在 Rails 上存在错误 Ruby
Creartor must exist error Ruby on Rails
我有两个模型,问题和用户
用户:
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
结束
在我得到的表格中,他的字段 Creator 必须存在,但我在插入中看到它很好:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"2yY/0x6961iIb9PxcyQAKHUSRqEj+zwQ91uPwHibU9tWjEXqiKMqp3vwzI70FVI2agtYhPgljFPDZjVZV4cmyg==", "issue"=>{"title"=>"asdsad", "description"=>"asdsa", "creator_id"=>"2", "tipo"=>"adas", "prioridad"=>"dsdaasd", "assigned_id"=>"3"}, "commit"=>"Create Issue"}
Unpermitted parameter: :creator_id
(0.1ms) begin transaction
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
(0.1ms) rollback transaction
有帮助吗?
您的 creator_id 未列入白名单:
Unpermitted parameter: :creator_id
你应该 post 你正在做的控制器代码 params.require(...).permit(...)
这样我们就可以看到那里有什么。
它应该看起来像:
def issue_params
params.
require(:issue).
permit(:title, :description, :creator_id, :tipo, :prioridad, :assigned_id)
end
如果您使用的是 strong_parameters
gem,那么 jvillian 的回答将有效。如果不是(这是完全有可能的,因为它直到 Rails 4 才被广泛使用),您可能需要将 attr_accessible :creator_id
添加到您的问题模型中。
我有两个模型,问题和用户
用户:
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
结束
在我得到的表格中,他的字段 Creator 必须存在,但我在插入中看到它很好:
Parameters:
{"utf8"=>"✓", "authenticity_token"=>"2yY/0x6961iIb9PxcyQAKHUSRqEj+zwQ91uPwHibU9tWjEXqiKMqp3vwzI70FVI2agtYhPgljFPDZjVZV4cmyg==", "issue"=>{"title"=>"asdsad", "description"=>"asdsa", "creator_id"=>"2", "tipo"=>"adas", "prioridad"=>"dsdaasd", "assigned_id"=>"3"}, "commit"=>"Create Issue"}
Unpermitted parameter: :creator_id
(0.1ms) begin transaction
User Load (0.3ms)SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
(0.1ms) rollback transaction
有帮助吗?
您的 creator_id 未列入白名单:
Unpermitted parameter: :creator_id
你应该 post 你正在做的控制器代码 params.require(...).permit(...)
这样我们就可以看到那里有什么。
它应该看起来像:
def issue_params
params.
require(:issue).
permit(:title, :description, :creator_id, :tipo, :prioridad, :assigned_id)
end
如果您使用的是 strong_parameters
gem,那么 jvillian 的回答将有效。如果不是(这是完全有可能的,因为它直到 Rails 4 才被广泛使用),您可能需要将 attr_accessible :creator_id
添加到您的问题模型中。