我看过accepts_nested_attributes
here和here之前的SO解决方案,但我仍然收到错误消息。我使用React作为前端和Rails后端。我正在尝试创建一个请求发送到计划,并从那里填充到工作人员。Rails 5 accept_nested_attributes_for未经许可的参数错误? (React前端)
我正在使用Rails 5.0.2。我有一个schedule, worker, roster
模型。
//Schedule
has_many :workers, through: :rosters, dependent: :destroy
has_many :rosters
accepts_nested_attributes_for :workers #implement accept_nested_attributes here
//Roster
belongs_to :schedule
belongs_to :worker
//Worker
has_many :schedules, through: :rosters
has_many :rosters
这里是我的日程表控制器:
def create
@schedule = Schedule.new(schedule_params)
if @schedule.save
render json: @schedule
else
render json: @schedule, status: :unprocessable_entity
end
end
...
private
def schedule_params
params.permit(:date, :user_id, :workers_attributes => [:worker_id, :name, :phone])
end
这里是我得到的错误:
app/controllers/schedules_controller.rb:13:in `create'
Started POST "/api/schedules" for 127.0.0.1 at 2017-05-23 10:30:38 -0700
Processing by SchedulesController#create as */*
Parameters: {"date"=>"2017-05-25T02:00:00.000Z", "user_id"=>1, "workers_attributes"=>{"name"=>"Iggy Test", "phone"=>"1
23-456-7890"}, "schedule"=>{"date"=>"2017-05-25T02:00:00.000Z", "user_id"=>1}}
Unpermitted parameter: schedule
Completed 500 Internal Server Error in 15ms (ActiveRecord: 0.0ms)
TypeError (no implicit conversion of Symbol into Integer):
app/controllers/schedules_controller.rb:13:in `create'
为什么我的要求显示Unpermitted parameter schedule
?如果我删除workers_attributes
并且只有params.permit(:date, :user_id)
,它就可以工作。我无法弄清楚为什么错误指向计划。我怎样才能让成功的POST nested_attributes请求导轨?
我使用取做POST请求从侧面反应:
...
return fetch(`api/schedules`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
date: date,
user_id: 1,
workers_attributes: {name: "Iggy Test", phone: "123-456-7890"}
})
编辑:
从@gabrielhilal以下的答案后,我加入require()
:params.require(:schedule).permit(:date, :user_id, :workers_attributes => [:id, :name, :phone])
,和编辑获取POST上React的结尾有数组对象而不是普通对象:workers_attributes: [{name: "Iggy Test", phone: "123-456-7890"}]
。它不再抱怨了,它确实注册新的时间表。然而,新的工人都是nil
:
#Worker.last shows:
#<Worker id: 32, name: nil, phone: nil, created_at: "2017-05-23 19:36:09", updated_at: "2017-05-23 19:36:09">
对不起,不是说要创建嵌套的问题,但没有人知道为什么它是零?
编辑2:
我得到它的工作排序。
如果我有
def create
@schedule = Schedule.new(schedule_params)
@workers = @schedule.rosters.build.build_worker
...
和
//schedule_params
params.permit(:date, :user_id, :workers_attributes => [:id, :name, :pho
NE])
我能有"Iggy Test"
显示,但它会立即创建另一个nil
工人。
登录:
Started POST "/api/schedules" for 127.0.0.1 at 2017-05-23 20:42:38 -0700
Processing by SchedulesController#create as */*
Parameters: {"date"=>"2017-05-26T02:00:00.000Z", "user_id"=>1, "workers_attributes"=>[{"name"=>"Iggy Test", "phone"=>"
123-456-7890"}], "schedule"=>{"date"=>"2017-05-26T02:00:00.000Z", "user_id"=>1}}
Unpermitted parameter: schedule
(0.1ms) begin transaction
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
SQL (1.1ms) INSERT INTO "schedules" ("date", "created_at", "updated_at", "user_id") VALUES (?, ?, ?, ?) [["date", 20
17-05-26 02:00:00 UTC], ["created_at", 2017-05-24 03:42:38 UTC], ["updated_at", 2017-05-24 03:42:38 UTC], ["user_id", 1]
]
SQL (0.2ms) INSERT INTO "workers" ("name", "phone", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "Iggy
Test"], ["phone", "123-456-7890"], ["created_at", 2017-05-24 03:42:38 UTC], ["updated_at", 2017-05-24 03:42:38 UTC]]
SQL (0.2ms) INSERT INTO "rosters" ("worker_id", "created_at", "updated_at") VALUES (?, ?, ?) [["worker_id", 56], ["c
reated_at", 2017-05-24 03:42:38 UTC], ["updated_at", 2017-05-24 03:42:38 UTC]]
SQL (0.1ms) INSERT INTO "workers" ("created_at", "updated_at") VALUES (?, ?) [["created_at", 2017-05-24 03:42:38 UTC
], ["updated_at", 2017-05-24 03:42:38 UTC]]
SQL (0.6ms) INSERT INTO "rosters" ("schedule_id", "worker_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["sc
hedule_id", 52], ["worker_id", 57], ["created_at", 2017-05-24 03:42:38 UTC], ["updated_at", 2017-05-24 03:42:38 UTC]]
SQL (0.4ms) UPDATE "rosters" SET "schedule_id" = ?, "updated_at" = ? WHERE "rosters"."id" = ? [["schedule_id", 52],
["updated_at", 2017-05-24 03:42:38 UTC], ["id", 52]]
(5.6ms) commit transaction
Completed 200 OK in 417ms (Views: 6.9ms | ActiveRecord: 14.7ms)
如果我修改PARAMS有需要(:时间表)
params.require(:schedule).permit(:date, :user_id, :workers_attributes => [:id, :name, :phone])
它创建只是一个nil
工人。
登录:
Started POST "/api/schedules" for 127.0.0.1 at 2017-05-23 20:45:03 -0700
Processing by SchedulesController#create as */*
Parameters: {"date"=>"2017-05-26T02:00:00.000Z", "user_id"=>1, "workers_attributes"=>[{"name"=>"Iggy Test", "phone"=>"
123-456-7890"}], "schedule"=>{"date"=>"2017-05-26T02:00:00.000Z", "user_id"=>1}}
(0.1ms) begin transaction
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
SQL (0.4ms) INSERT INTO "schedules" ("date", "created_at", "updated_at", "user_id") VALUES (?, ?, ?, ?) [["date", 20
17-05-26 02:00:00 UTC], ["created_at", 2017-05-24 03:45:03 UTC], ["updated_at", 2017-05-24 03:45:03 UTC], ["user_id", 1]
]
SQL (0.2ms) INSERT INTO "workers" ("created_at", "updated_at") VALUES (?, ?) [["created_at", 2017-05-24 03:45:03 UTC
], ["updated_at", 2017-05-24 03:45:03 UTC]]
SQL (0.3ms) INSERT INTO "rosters" ("schedule_id", "worker_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["sc
hedule_id", 53], ["worker_id", 58], ["created_at", 2017-05-24 03:45:03 UTC], ["updated_at", 2017-05-24 03:45:03 UTC]]
(2.4ms) commit transaction
Completed 200 OK in 81ms (Views: 1.3ms | ActiveRecord: 8.3ms)
嗨@gabrielhilal,你的答案有助于解决一个问题 - 谢谢!我在我的params方法中添加了require(),现在我有:'params.require(:schedule).permit(:date,:user_id,:workers_attributes => [:id,:name,:phone])''。它修复了'schedule'错误。我还将'workers_attributes'的POST请求更改为对象数组:'workers_attributes:[{name:“Iggy Test”,phone:“123-456-7890”}]'。一个问题是,当我做'Worker.last'时,它显示'nil'属性:'<工号:32,名字:无,电话:无,created_at:“2017-05-23 19:36:09”, updated_at:“2017-05-23 19:36:09”>。你知道为什么吗? – Iggy
看着你的'params'我不认为你需要'params.require(:schedule)',除非你在'schedule'内发送所有东西,比如'{“schedule”=> {“date”=> 2017-05-25T02:00:00.000Z“,”user_id“=> 1,”workers_attributes“=> {...}}}'... – gabrielhilal
当我删除它时,它对最终没有任何影响结果/数据库中保存的内容。 “Worker.last”仍然会为名称和电话显示“nil”。看来Rails没有意识到参数在那里。新创建的Worker只有'id','updated_at'和'created_at'。 – Iggy