加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – “无法使用嵌套属性批量分配受保护的属性”

发布时间:2020-12-17 03:46:45 所属栏目:百科 来源:网络整理
导读:我已经看到了这个问题的其他问题,但到目前为止,答案对我没有用.我正在尝试使用一个注册用户的表单,并同时创建一个组织.用户和组织通过分配表关联. 这是我的表格: = form_for(resource,:as = resource_name,:url = registration_path(resource_name)) do |f|
我已经看到了这个问题的其他问题,但到目前为止,答案对我没有用.我正在尝试使用一个注册用户的表单,并同时创建一个组织.用户和组织通过分配表关联.

这是我的表格:

= form_for(resource,:as => resource_name,:url => registration_path(resource_name)) do |f|

  = devise_error_messages!

  = f.fields_for :organizations do |f|

    = f.label :name
    = f.text_field :name

  = f.label :email
  = f.email_field :email

  = f.label :password
  = f.password_field :password

  = f.label :password_confirmation
  = f.password_field :password_confirmation

我的注册控制器:

class Users::RegistrationsController < Devise::RegistrationsController
  def new
    @user = User.new
    @user.organizations.build
  end

  def create
    super
  end

  def update
    super
  end
end

我的组织模型:

class Organization < ActiveRecord::Base
  has_many :organization_assignments
  has_many :users,:through => :organization_assignments

  attr_accessible :name
end

和我的用户模型:

class User < ActiveRecord::Base

  has_many :organization_assignments
  has_many :organizations,:through => :organization_assignments

  # Include default devise modules. Others available are:
  # :token_authenticatable,:confirmable,# :lockable,:timeoutable and :omniauthable
  devise :database_authenticatable,:registerable,:recoverable,:rememberable,:trackable,:validatable

  accepts_nested_attributes_for :organizations

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email,:password,:password_confirmation,:remember_me,:organization_attributes
  # attr_accessible :title,:body

end

我得到的确切错误是:

Can’t mass-assign protected attributes: organizations_attributes

解决方法

您必须在User模型中将organizational_attributes添加到attr_accessible.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读