为 nil:NilClass 设计未定义的方法 `build'
Devise undefined method `build' for nil:NilClass
我目前正在 rails 参加 ruby 的在线课程,我正在从头开始学习所有内容,如果我的问题不清楚,请原谅我。这是我的 delima,我正在尝试在 rails 应用程序中的两个模型之间创建一个 LINK,这是我目前所拥有的。但是,当我尝试访问 localhost3000/business/new 时,它 returns 错误提到了它的标题。我得出的结论是,这是因为我使用了 "has_one :model" 类型的关联,而不是 "has_many :model"。我希望有人能在这里为我指明正确的方向,因为我已经花了数小时寻找解决方案。
class BusinessesController < ApplicationController
before_action :set_business, only: [:show, :edit, :update, :destroy]
//edited for clarity
def new
@business = current_user.business.build //The line that returns a undefined method for nil class
end
def edit
end
def create
@business = current_user.business.build(business_params)
if @business.save
redirect_to @business, notice: 'Business was successfully created.'
else
render "new"
end
end
设计用户模型
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :buzzs, dependent: :destroy
has_one :business, dependent: :destroy
end
商业模式
class Business < ActiveRecord::Base
belongs_to :user
end
看看这个http://guides.rubyonrails.org/association_basics.html#has-one-association-reference。
您应该使用 build_business
而不是 business.build
4.2.1 has_one
添加的方法
当你声明一个has_one关联时,声明class会自动获得与该关联相关的五个方法:
association(force_reload = false)
association=(associate)
build_association(attributes = {})
create_association(attributes = {})
create_association!(attributes = {})
一对一协会:
class User < ActiveRecord::Base
has_one :business
end
current_user.build_business
或者一对多:
class User < ActiveRecord::Base
has_many :businesses
end
current_user.businesses.build
我目前正在 rails 参加 ruby 的在线课程,我正在从头开始学习所有内容,如果我的问题不清楚,请原谅我。这是我的 delima,我正在尝试在 rails 应用程序中的两个模型之间创建一个 LINK,这是我目前所拥有的。但是,当我尝试访问 localhost3000/business/new 时,它 returns 错误提到了它的标题。我得出的结论是,这是因为我使用了 "has_one :model" 类型的关联,而不是 "has_many :model"。我希望有人能在这里为我指明正确的方向,因为我已经花了数小时寻找解决方案。
class BusinessesController < ApplicationController
before_action :set_business, only: [:show, :edit, :update, :destroy]
//edited for clarity
def new
@business = current_user.business.build //The line that returns a undefined method for nil class
end
def edit
end
def create
@business = current_user.business.build(business_params)
if @business.save
redirect_to @business, notice: 'Business was successfully created.'
else
render "new"
end
end
设计用户模型
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :buzzs, dependent: :destroy
has_one :business, dependent: :destroy
end
商业模式
class Business < ActiveRecord::Base
belongs_to :user
end
看看这个http://guides.rubyonrails.org/association_basics.html#has-one-association-reference。
您应该使用 build_business
而不是 business.build
4.2.1 has_one
添加的方法当你声明一个has_one关联时,声明class会自动获得与该关联相关的五个方法:
association(force_reload = false)
association=(associate)
build_association(attributes = {})
create_association(attributes = {})
create_association!(attributes = {})
一对一协会:
class User < ActiveRecord::Base
has_one :business
end
current_user.build_business
或者一对多:
class User < ActiveRecord::Base
has_many :businesses
end
current_user.businesses.build