Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 设计和多态关联_Ruby On Rails_Devise_Ruby On Rails 3.2 - Fatal编程技术网

Ruby on rails 设计和多态关联

Ruby on rails 设计和多态关联,ruby-on-rails,devise,ruby-on-rails-3.2,Ruby On Rails,Devise,Ruby On Rails 3.2,我正在尝试使用Desive构建一个应用程序,它提供一个登录和注销界面,但根据注册模式提供多个注册界面。因此,我发现多态关联非常适合我的需求,因为我希望为不同的模型提供单独的表 但是我无法注册任何用户,因为我总是会在基本用户模型的属性中遇到一些错误。我读过书,我想我已经适应了一切 基本用户模型: class User < ActiveRecord::Base # Include default devise modules. Others available are: # :toke

我正在尝试使用Desive构建一个应用程序,它提供一个登录和注销界面,但根据注册模式提供多个注册界面。因此,我发现多态关联非常适合我的需求,因为我希望为不同的模型提供单独的表

但是我无法注册任何用户,因为我总是会在基本用户模型的属性中遇到一些错误。我读过书,我想我已经适应了一切

基本用户模型:

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

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :role_id, :role_type,
                  :salutation, :firstname, :lastname, :street, :zipcode, :city, :phone

  belongs_to :role, polymorphic: true

  def customer?
    'Customer' == self.role_type
  end

  def customer
    Customer.find(self.role_id) if self.customer?
  end

  def admin?
    'Admin' == self.role_type
  end

  def admin
    Admin.find(self.role_id) if self.admin?
  end
end
参数包括:

{"utf8"=>"✓",
 "authenticity_token"=>"WngmLBR7MmVuhDyYxsASWQscUTB/VFmKmn/fdatVB30=",
 "customer"=>{"user"=>{"email"=>"max@mustermann.com",
 "password"=>"[FILTERED]",
 "password_confirmation"=>"[FILTERED]",
 "salutation"=>"",
 "firstname"=>"Max",
 "lastname"=>"Mustermann",
 "street"=>"Musterstraße 1",
 "zipcode"=>"12345",
 "city"=>"Musterstadt",
 "phone"=>"4917620289872"}},
 "commit"=>"Create Customer"}
我认为错误在Customer类中,但我不知道如何修复它

更新 我对客户模型进行了更改:

class Customer < ActiveRecord::Base
  has_one :user, as: :role
  attr_accessible :user_attributes
  accepts_nested_attributes_for :user
end
class Customer < ActiveRecord::Base
  has_one :user, as: :role
  attr_accessible :user # :user instead of :user_attributes
  accepts_nested_attributes_for :user
end
也许对你有帮助 代替
attr\u可访问:用户属性
attr\u可访问:用户

在客户模型中

更好的方法:---


application.rb存在config.active\u record.whitelist\u attributes=true

请参阅

提前阅读

在您的视野中:

f.fields_for :user do |u|
#...
而不是

f.fields_for @customer.user do |u|

发生错误的原因是嵌套的根属性应该是“user\u attributes”,而不是“user”

粘贴您的
车库
型号代码。您发送的参数是
“客户”=>{“用户”=>{
,但它们应该是
“客户”=>{“用户属性”=>{“
…您可能需要发布创建用户的表单的视图代码以帮助诊断。好的,我编辑了我的答案。它现在应该可以工作了。这给了我另一个错误:ActiveRecord::CustomerController中的AssociationTypeMismatch#创建用户(#70271634398080)预期,获得ActiveSupport::HashWithInferenceAccess(#70271609254480)config.active\u record.whitelist\u attributes设置为true这是关联错误从现在起将不会显示批量分配错误。请检查关联客户是否有一个:用户,因此用户属于:客户,但在您的用户模型中,用户属于:角色谢谢
class Customer < ActiveRecord::Base
  has_one :user, as: :role
  attr_accessible :user # :user instead of :user_attributes
  accepts_nested_attributes_for :user
end
ActiveRecord::AssociationTypeMismatch in CustomersController#create
User(#70271634657140) expected, got ActiveSupport::HashWithIndifferentAccess(#70271609254480)

app/controllers/customers_controller.rb:44:in `new'
app/controllers/customers_controller.rb:44:in `create'
f.fields_for :user do |u|
#...
f.fields_for @customer.user do |u|