Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.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 未定义的方法`地址';对于#<;用户:0x007fc955029380>;_Ruby On Rails_Ruby_Undefined_Rails Activerecord - Fatal编程技术网

Ruby on rails 未定义的方法`地址';对于#<;用户:0x007fc955029380>;

Ruby on rails 未定义的方法`地址';对于#<;用户:0x007fc955029380>;,ruby-on-rails,ruby,undefined,rails-activerecord,Ruby On Rails,Ruby,Undefined,Rails Activerecord,我是Ruby和Rails的新手,尝试修复我经常遇到的错误。我不知道怎么修。请帮忙 Route.rb namespace :my do namespace :account do resource :details, :only => [:show, :update] resources :addresses end end 地址控制器 class My::Account::AddressesController < MyControlle

我是Ruby和Rails的新手,尝试修复我经常遇到的错误。我不知道怎么修。请帮忙

Route.rb

namespace :my do
    namespace :account do
      resource  :details, :only => [:show, :update]
      resources :addresses
    end
end 
地址控制器

class My::Account::AddressesController < MyController

  def index
    @addresses = current_user.addresses
  end

  def new
    @address = current_user.addresses.new
  end

....

end
User.rb

class User < ActiveRecord::Base
    validates :email, :presence => true, :uniqueness => true
    has_secure_password
 ...
end 
class用户true,:university=>true
有安全的密码吗
...
结束
Customer.rb

class Customer < User

    has_many :addresses

    def self.register(attributes)
        customer = create!(attributes)
        return customer
    end

    def full_name
        "#{first_name} #{last_name}"
    end

end
class客户
Address.rb

class Customer::Address < ActiveRecord::Base

  belongs_to :customer 

  self.table_name = 'customer_addresses'

  default_scope { where(:deleted_at => nil) }

  validates :line_1, :postcode, :phone, :presence => true

end
class Customer::Addressnil)}
验证:行_1,:邮政编码,:电话,:状态=>true
结束

您需要在两个模型上定义关系

class User < ActiveRedord::Base
  has_many :addresses, class_name: 'CustomerAddress', foreign_key: 'customer_id'
end

class Address < ActiveRecord::Base
  belongs_to :user, foreign_key: 'customer_id'
end
class用户
您需要在两个模型上定义关系

class User < ActiveRedord::Base
  has_many :addresses, class_name: 'CustomerAddress', foreign_key: 'customer_id'
end

class Address < ActiveRecord::Base
  belongs_to :user, foreign_key: 'customer_id'
end
class用户
#current_customer
方法添加到
应用程序控制器
,该方法返回
客户
-实例,而不是
用户
-实例:

class ApplicationController
  #…
  private
  def current_customer
    current_user && Customer.find_by_id(current_user.id)
  end
end
然后按如下方式更改代码:

class My::Account::AddressesController < MyController

  def index
    @addresses = current_customer.addresses
  end

  def new
    @address = current_customer.addresses.new
  end

  #…
end
class My::Account::AddressesController
#current_customer
方法添加到
应用程序控制器
,该方法返回
客户
-实例,而不是
用户
-实例:

class ApplicationController
  #…
  private
  def current_customer
    current_user && Customer.find_by_id(current_user.id)
  end
end
然后按如下方式更改代码:

class My::Account::AddressesController < MyController

  def index
    @addresses = current_customer.addresses
  end

  def new
    @address = current_customer.addresses.new
  end

  #…
end
class My::Account::AddressesController
您能显示您的地址和用户型号吗?作为旁注,为什么要使用这么多的名称空间?hi@dax将型号添加到post@musre:名称空间-让“我的帐户”远离其他帐户/保持其整洁,这不是一个好主意吗?同样感谢您的回答,但是我的模型已经有了关系-添加到上面的帖子中。
当前用户
可能是
用户
-实例,而不是
客户
-实例。您能显示您的地址和用户模型吗?作为旁注,为什么要使用这么多的名称空间?hi@dax将模型添加到post@musre:名称空间-让“我的帐户”远离其他帐户/保持其整洁,这不是一个好主意吗?同样感谢您的回答,但是我的模型已经有了关系-添加到上面的帖子中。
当前用户
可能是
用户
-实例,而不是
客户
-实例。在Rails 4中,您可以使用class:CustomerAddress在Rails 4中,您可以使用class:CustomerAddress