ruby-on-rails – Rails:ActiveSupport的未定义方法`primary_ke
发布时间:2020-12-17 02:21:27 所属栏目:百科 来源:网络整理
导读:我的用户模型有多态关系.我正在使用Devise. 当我尝试编辑用户的详细信息时,我收到以下错误: undefined method `primary_key' for ActiveSupport::HashWithIndifferentAccess:Class 通过表格提交的数据是: {"utf8"="?","_method"="put","authenticity_token
我的用户模型有多态关系.我正在使用Devise.
当我尝试编辑用户的详细信息时,我收到以下错误: undefined method `primary_key' for ActiveSupport::HashWithIndifferentAccess:Class 通过表格提交的数据是: {"utf8"=>"?","_method"=>"put","authenticity_token"=>"Ap0JP2bs/w9J6iI9rZahiKR1K8UEIi7rp33a4OutMbo=","user"=>{"email"=>"some_email@yahoo.com","rolable"=>{"first_name"=>"Cristian","last_name"=>"Gavrila","city"=>"16"}},"commit"=>"Update"} 控制器方法是: def update @user = User.find(current_user.id) if @user.update_attributes(params[:user]) redirect_to edit_user_registration_path,notice: 'Your profile was successfully updated.' else redirect_to edit_user_registration_path,error: "Something went wrong,couldn't update your profile!." end end 型号: 1.用户 class User < ActiveRecord::Base belongs_to :rolable,:polymorphic => true # Devise business devise :database_authenticatable,:registerable,:recoverable,:rememberable,:trackable,:validatable # Setup accessible (or protected) attributes for your model attr_accessible :remote_avatar_url,:avatar,:login,:username,:email,:password,:password_confirmation,:remember_me # For authenticating with facebook attr_accessible :provider,:uid,:rolable devise :omniauthable,:omniauth_providers => [:facebook] # For authenticating with both username or email attr_accessor :login # Upload avatar mount_uploader :avatar,AvatarUploader # Makes username necessary validates :username,:presence => true end 2.客户 class Customer < ActiveRecord::Base has_one :user,:as => :rolable has_one :preferences,:class_name => "CustomerPreferences" belongs_to :city attr_accessible :first_name,:last_name end 有什么问题? 解决方法
根据您的请求哈希,您将传递rolable属性:
"rolable"=>{"first_name"=>"Cristian","city"=>"16"} 除非指定User模型接受rolable的嵌套属性,否则不能这样做.但是,您已将用户设置为属于Rolable而不是相反.嵌套属性不是为处理这种反向关系而设计的 – 您可能需要重新考虑您要在此处完成的操作,并相应地修改关系. 例如,您可能希望将哈希内部转出,并使用嵌入的用户属性传递可滚动属性.或者您可能希望将rolable转换为has_one关联. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |