簡體   English   中英

accepts_nested_attributes_for無法在Rails中為用戶模型按預期工作

[英]accepts_nested_attributes_for not working as expected for User model in Rails

嘗試提交嵌套屬性時出現以下錯誤:

Can't mass-assign protected attributes: staff

我的User模型(由Devise創建):

class User < ActiveRecord::Base
  attr_accessible :name
  has_one :staff
  accepts_nested_attributes_for :staff, reject_if: :new_record?
end

我的Devise registrations/edit.html.erb視圖:

<%= f.fields_for resource.staff do |s| %>
  <%= s.label :notification_email %>
  <%= s.email_field :notification_email, placeholder: 'Enter notification email' %>
<% end %>

HTML帖子中作為參數提交的內容:

"user"=>{"name"=>"Scott",
"staff"=>{"notification_email"=>"myemail@example.com"}}

根據嵌套屬性文檔,我期望人員的HTML post參數為"staff_attributes"而不僅僅是"staff"

我做錯什么了嗎? 我是否真的需要將staff添加到attr_accessible屬性列表中? 這不適用於Devise嗎? 我確實必須在視圖中使用resource而不是@user

在您看來,刪除resource. 如下:

<%= f.fields_for :staff do |s| %>

在模型中,將:staff_attribute (或staff_attributes )添加到attr_accessible

  1. 傳遞符號而不是實例。

  2. 在用戶模型中添加attr_accessible :staff_attributes

  3. 在用戶控制器的新方法中初始化人員實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM