2017-05-23 66 views
0

我看過accepts_nested_attributesherehere之前的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) 

回答

1

您的訊息:

{ 
    "date"=>"2017-05-25T02:00:00.000Z", 
    "user_id"=>1, 
    "workers_attributes"=>{ 
    "name"=>"Iggy Test", 
    "phone"=>"123-456-7890" 
    }, 
    "schedule"=> { 
    "date"=>"2017-05-25T02:00:00.000Z", 
    "user_id"=>1 
    } 
} 

你有兩個問題:

  • schedule是不允許的(這就是爲什麼你看到日誌中的消息),但它會被忽略(不會產生任何錯誤)。
  • workers_attributes應該是一個工人的集合,而不是一個簡單的散列,所以這就是爲什麼你有錯誤。

你應該得到的東西就像郵局要求如下:

{ 
    "date"=>"2017-05-25T02:00:00.000Z", 
    "user_id"=>1, 
    "workers_attributes"=>{ 
    "0" => { 
     "name"=>"Iggy Test", 
     "phone"=>"123-456-7890" 
    } 
    } 
} 
+0

嗨@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

+0

看着你的'params'我不認爲你需要'params.require(:schedule)',除非你在'schedule'內發送所有東西,比如'{「schedule」=> {「date」=> 2017-05-25T02:00:00.000Z「,」user_id「=> 1,」workers_attributes「=> {...}}}'... – gabrielhilal

+0

當我刪除它時,它對最終沒有任何影響結果/數據庫中保存的內容。 「Worker.last」仍然會爲名稱和電話顯示「nil」。看來Rails沒有意識到參數在那裏。新創建的Worker只有'id','updated_at'和'created_at'。 – Iggy