![](/img/trans.png)
[英]How can I solve the following error: NameError: undefined local variable or method `email' for #<AuthenticateUser>
[英]How to solve undefined local variable or method error
我有三个表: RM
, GenericUser
和Properties
。
RM
栏位: id
, name
GenericUser
字段: id
, mobile
, rm_id
, name
。
Properties
字段id
, image
, rm_id
, generic_user_id
, property_name
。
RM
正在创建GenericUser
,因此rm_id
将自动输入GenericUser
表中。
该普通用户登录并创建属性后,因此,当普通用户创建属性时, rm_id
应从普通用户表中获取,并应在rm_id
所在的属性表字段中rm_id
。
我有这样的模型关系。
class Rm < ActiveRecord::Base
acts_as :user
has_many :generic_users
end
class Property < ActiveRecord::Base
belongs_to :generic_user
end
class GenericUser < ActiveRecord::Base
belongs_to :rm
has_many :properties
end
我的generic_users propertiescontroller
控制器:
def create
@property = Property.new(property_params)
@gu=GenericUser.select(:rm_id).where(:id=>current_user.specific.id)
@property.rm_id=@gu.rm_id
logger.info @gu.inspect
logger.info @property.rm_id.inspect
if @property.save
redirect_to :back, notice: 'Property was successfully created.'
else
render :new
end
end
我收到此错误:
当我检查@gu iam得到这个
#<ActiveRecord::Relation [#<GenericUser id: 1, mobile: "8776766777", address: "reewtrwt", created_at: "2016-02-18 07:41:06", updated_at: "2016-02-19 08:59:13", dob: "", rm_id: "4", category_id: "1",>]> Completed 500 Internal Server Error in 73ms (ActiveRecord: 1.9ms)
我如何将这个GenericUser rm_id(即rm_id:4)保存到属性表字段rm_id?
您的错误在于以下几行:
@gu = GenericUser.select(:rm_id).where(:id=>current_user.specific.id)
@property.rm_id = @gu.rm_id
说明:
@gu
作为Active Record Relations
返回,因此您无法调用@gu.rm_id
解决方案 -尝试如下操作:
@gu = GenericUser.find(current_user.specific.id)
@property.rm_id = @gu.rm_id
@gu
现在是GenericUser
,因此您可以在其上调用rm_id
您已经在@gu中选择rm_id。 您无需再次调用它。
改变这个
@property.rm_id=@gu.rm_id
为此,
@property.rm_id = @gu
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.