Ruby on rails EventsController中的NoMethodError#创建未定义的方法`build';零:零级?
我有点困惑,为什么我会收到这个错误,因为(我认为)它表明我当前的客户返回的是零。我不知道如何解决这个问题,这是我当前的客户方法、会话控制器或事件控制器的问题吗 事件模型:Ruby on rails EventsController中的NoMethodError#创建未定义的方法`build';零:零级?,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,nomethoderror,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,Nomethoderror,我有点困惑,为什么我会收到这个错误,因为(我认为)它表明我当前的客户返回的是零。我不知道如何解决这个问题,这是我当前的客户方法、会话控制器或事件控制器的问题吗 事件模型: class Event < ActiveRecord::Base belongs_to :calendar end 日历模型: class Calendar < ActiveRecord::Base belongs_to :customer has_many :events end class日
class Event < ActiveRecord::Base
belongs_to :calendar
end
日历模型:
class Calendar < ActiveRecord::Base
belongs_to :customer
has_many :events
end
class日历
日历\u控制器:
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 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
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 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
class CalendarsController
我是Ruby/Rails新手,因此我非常感谢您的解释和回答,请帮助!谢谢
@calendar = current_customer.calendar
@event = @calendar.build.event(event_params)
根据错误消息,@calendar
是nil
,因此当您在下一行调用@calendar.build.event(event_params)
时,它会调用:nil.build
,因此您会得到错误
出于某种原因,您的当前客户的日历对于该请求为nil
。找出这一点,并确保当前客户
在数据库中有一个日历
,然后它就可以工作了
此外,您还需要更改:
@event = @calendar.build.event(event_params)
致:
在事件控制器中,@calendar似乎为零
首先检查当前客户是否返回了您期望的客户
然后检查当前客户是否创建了日历供您参考 好吧,我真傻,我在一个老客户身上测试它,这个客户是在我实现日历之前创建的。现在,EventsController#create undefined method'build'for#中的错误已更改为NoMethodError。这是语法错误吗?请看这篇文章。我认为您的语法有点不正确``@event=@calendar.event.build(event\u params)``好的,我意识到我是在一个老客户身上测试它的。现在calendar不是nil,但我仍然得到错误:EventsController中的NoMethodError#在同一行上为#创建未定义的方法'build'。这是语法错误吗?请尝试:@calendar.events.build(event\u params)
。这不是复制问题所需的最小代码量。
@event = @calendar.build.event(event_params)
@event = @calendar.events.build(event_params)