繁体   English   中英

如何解决未定义的局部变量或方法错误

[英]How to solve undefined local variable or method error

我有三个表: RMGenericUserProperties

RM栏位: idname

GenericUser字段: idmobilerm_idname

Properties字段idimagerm_idgeneric_user_idproperty_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM