gpt4 book ai didi

ruby-on-rails - "Can' t 批量分配 protected 属性“具有嵌套属性

转载 作者:行者123 更新时间:2023-12-03 00:32:29 24 4
gpt4 key购买 nike

我已经看到了与此问题相关的其他问题,但到目前为止,答案对我不起作用。我正在尝试创建一个注册用户并同时创建组织的表单。用户和组织通过分配表关联。

这是我的表格:

= 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

我的注册 Controller :

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

最佳答案

您必须将 :organizations_attributes 添加到 User 模型中的 attr_accessible

关于ruby-on-rails - "Can' t 批量分配 protected 属性“具有嵌套属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11405065/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com