EventsController 中的 NoMethodError#create undefined method ‘build’ for nil:NilClass?
NoMethodError in EventsController#create undefined method `build' for nil:NilClass?
我对为什么会收到此错误感到有点困惑,因为(我认为)这表明我的 current_customer returns 为零。我不知道如何解决这个问题,这是我的 current_customer 方法、会话控制器还是我的 events_controller...
的问题
事件模型:
class Event < ActiveRecord::Base
belongs_to :calendar
end
事件控制器:
class EventsController < ApplicationController
def new
@event = Event.new
@calendar = current_customer.calendar #WHY IS IT NOT ERRORING HERE TOO?
end
def create
**@calendar = current_customer.calendar #IT IS CALLING ERROR HERE**
@event = @calendar.build.event(event_params)
if @event.save
redirect_to '/main'
else
redirect_to '/compose'
end
end
private
def event_params
params.require(:event).permit(:calendar_id, :name, :starts_at, :ends_at)
end
客户模型:
class Customer < ActiveRecord::Base
belongs_to :business
has_one :calendar
has_secure_password
attr_accessor :remember_token
#remembers a user in the database for use in persistent sessions
def remember
self.remember_token = Customer.new_token
update_attribute(:remember_digest, Customer.digest(remember_token))
end
def Customer.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
def forget
update_attribute(:remember_digest, nil)
end
def Customer.new_token
SecureRandom.urlsafe_base64
end
#returns true if the given token matches the digest
def authenticated?(remember_token)
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end
end
客户控制员:
class CustomersController < ApplicationController
def new
@customer = Customer.new
@businesses = Business.all
@calendar = Calendar.new
end
def create
@customer = Customer.create(customer_params)
@calendar = @customer.build_calendar
@customer.save!
session[:customer_id] = @customer.id
redirect_to '/'
rescue ActiveRecord::RecordInvalid => ex
render action: 'new', alert: ex.message
end
private
def customer_params
params.require(:customer).permit(:first_name, :last_name, :business_no, :email, :password, :business_id)
end
end
会话控制器:
class SessionsController < ApplicationController
def new
end
def create
@customer = Customer.find_by_email(params[:session][:email])
if @customer && @customer.authenticate(params[:session][:password])
session[:customer_id] = @customer.id #log in
@customer.remember
cookies.permanent.signed[:customer_id] = @customer.id
cookies.permanent[:remember_token] = @customer.remember_token
redirect_to '/main'
else
#need to add flash error: invalid password/email combination
redirect_to '/login'
end
end
def destroy
@current_customer.forget
cookies.delete(:customer_id)
cookies.delete(:remember_token)
session.delete(:customer_id)
@current_customer = nil
redirect_to '/'
end
end
Current_customer 方法 application.controller:
helper_method :current_customer
def current_customer
if (customer_id = session[:customer_id])
@current_customer ||= Customer.find_by(id: customer_id)
elsif (customer_id = cookies.signed[:customer_id])
customer = Customer.find_by(id: customer_id)
if customer && customer.authenticated?(cookies[:remember_token])
session[:customer_id] = customer.id #log in
@current_customer = customer
end
end
end
日历模型:
class Calendar < ActiveRecord::Base
belongs_to :customer
has_many :events
end
calendars_controller:
class CalendarsController < ApplicationController
def new
@calendar = Calendar.new(calendar_params)
end
def create
@calendar = Calendar.new(calendar_params)
end
private
def calendar_params
params.require(:customer_id)
end
end
我是 Ruby/Rails 的新手,所以非常感谢任何解释和答案,请帮忙!谢谢
@calendar = current_customer.calendar
@event = @calendar.build.event(event_params)
根据错误消息,@calendar
是 nil
,因此当您在下一行调用 @calendar.build.event(event_params)
时,它调用:nil.build
因此您得到错误。
出于某种原因,对于该请求,您的 current_customer
的 calendar
是 nil
。找出并确保 current_customer
在数据库中存在 calendar
,然后它应该可以工作。
此外,您需要更改:
@event = @calendar.build.event(event_params)
收件人:
@event = @calendar.events.build(event_params)
在事件控制器中,@calendar 似乎是 nil。
首先检查 current_customer 返回您预期的客户。
然后检查 current_customer 是否创建了供您参考的日历。
我对为什么会收到此错误感到有点困惑,因为(我认为)这表明我的 current_customer returns 为零。我不知道如何解决这个问题,这是我的 current_customer 方法、会话控制器还是我的 events_controller...
的问题事件模型:
class Event < ActiveRecord::Base
belongs_to :calendar
end
事件控制器:
class EventsController < ApplicationController
def new
@event = Event.new
@calendar = current_customer.calendar #WHY IS IT NOT ERRORING HERE TOO?
end
def create
**@calendar = current_customer.calendar #IT IS CALLING ERROR HERE**
@event = @calendar.build.event(event_params)
if @event.save
redirect_to '/main'
else
redirect_to '/compose'
end
end
private
def event_params
params.require(:event).permit(:calendar_id, :name, :starts_at, :ends_at)
end
客户模型:
class Customer < ActiveRecord::Base
belongs_to :business
has_one :calendar
has_secure_password
attr_accessor :remember_token
#remembers a user in the database for use in persistent sessions
def remember
self.remember_token = Customer.new_token
update_attribute(:remember_digest, Customer.digest(remember_token))
end
def Customer.digest(string)
cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
BCrypt::Engine.cost
BCrypt::Password.create(string, cost: cost)
end
def forget
update_attribute(:remember_digest, nil)
end
def Customer.new_token
SecureRandom.urlsafe_base64
end
#returns true if the given token matches the digest
def authenticated?(remember_token)
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end
end
客户控制员:
class CustomersController < ApplicationController
def new
@customer = Customer.new
@businesses = Business.all
@calendar = Calendar.new
end
def create
@customer = Customer.create(customer_params)
@calendar = @customer.build_calendar
@customer.save!
session[:customer_id] = @customer.id
redirect_to '/'
rescue ActiveRecord::RecordInvalid => ex
render action: 'new', alert: ex.message
end
private
def customer_params
params.require(:customer).permit(:first_name, :last_name, :business_no, :email, :password, :business_id)
end
end
会话控制器:
class SessionsController < ApplicationController
def new
end
def create
@customer = Customer.find_by_email(params[:session][:email])
if @customer && @customer.authenticate(params[:session][:password])
session[:customer_id] = @customer.id #log in
@customer.remember
cookies.permanent.signed[:customer_id] = @customer.id
cookies.permanent[:remember_token] = @customer.remember_token
redirect_to '/main'
else
#need to add flash error: invalid password/email combination
redirect_to '/login'
end
end
def destroy
@current_customer.forget
cookies.delete(:customer_id)
cookies.delete(:remember_token)
session.delete(:customer_id)
@current_customer = nil
redirect_to '/'
end
end
Current_customer 方法 application.controller:
helper_method :current_customer
def current_customer
if (customer_id = session[:customer_id])
@current_customer ||= Customer.find_by(id: customer_id)
elsif (customer_id = cookies.signed[:customer_id])
customer = Customer.find_by(id: customer_id)
if customer && customer.authenticated?(cookies[:remember_token])
session[:customer_id] = customer.id #log in
@current_customer = customer
end
end
end
日历模型:
class Calendar < ActiveRecord::Base
belongs_to :customer
has_many :events
end
calendars_controller:
class CalendarsController < ApplicationController
def new
@calendar = Calendar.new(calendar_params)
end
def create
@calendar = Calendar.new(calendar_params)
end
private
def calendar_params
params.require(:customer_id)
end
end
我是 Ruby/Rails 的新手,所以非常感谢任何解释和答案,请帮忙!谢谢
@calendar = current_customer.calendar
@event = @calendar.build.event(event_params)
根据错误消息,@calendar
是 nil
,因此当您在下一行调用 @calendar.build.event(event_params)
时,它调用:nil.build
因此您得到错误。
出于某种原因,对于该请求,您的 current_customer
的 calendar
是 nil
。找出并确保 current_customer
在数据库中存在 calendar
,然后它应该可以工作。
此外,您需要更改:
@event = @calendar.build.event(event_params)
收件人:
@event = @calendar.events.build(event_params)
在事件控制器中,@calendar 似乎是 nil。
首先检查 current_customer 返回您预期的客户。
然后检查 current_customer 是否创建了供您参考的日历。