我有点困惑,为什么我收到这个错误,因为(我认为)这表明我的current_customer返回nil。我不知道如何解决这个问题,我的current_customer方法,会话控制器或我的events_controller是一个问题......
活动模型:
uint WS_POPUP = 0x80000000;
protected override CreateParams CreateParams
{
get
{
CreateParams createParams = base.CreateParams;
createParams.Style |= unchecked((int)WS_POPUP);
return createParams;
}
}
事件控制器:
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
application.controller中的Current_customer方法:
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
日历模型:
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
calendars_controller:
class Calendar < ActiveRecord::Base
belongs_to :customer
has_many :events
end
我是Ruby / Rails的新手,所以我非常感谢任何解释和答案,请帮忙!感谢
答案 0 :(得分:1)
@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)
要:
suppressEvent
答案 1 :(得分:0)
似乎@calendar在事件控制器中是零。
首先检查current_customer会将您期望的客户转回给客户。
然后检查current_customer是否创建了一个日历供您参考。