Ruby on rails 3 nil的未定义方法:NilClass-Controller
请向我解释一下,为什么每次尝试创建预订时都会出现以下错误Ruby on rails 3 nil的未定义方法:NilClass-Controller,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails 3,Ruby On Rails 3.1,请向我解释一下,为什么每次尝试创建预订时都会出现以下错误 NoMethodError in BookingsController#create undefined method `diary_slot_day_time_path' for nil:NilClass 我的控制器中有以下代码: class BookingsController < PortalController # GET /bookings # GET /bookings.json def index
NoMethodError in BookingsController#create
undefined method `diary_slot_day_time_path' for nil:NilClass
我的控制器中有以下代码:
class BookingsController < PortalController
# GET /bookings
# GET /bookings.json
def index
session[:booking_context] = 'index'
if current_user.is_booking_manager?
# @bookings = Booking.all
# @bookings = Booking.filter(params[:search], [:reference_number, :company_id])
@bookings = Booking.includes(:company).filter(params[:search], [:reference_number, "companies.name"])
@main_heading = 'Bookings'
else
@bookings = current_user.company.bookings
@main_heading = "#{current_user.company.name} Bookings"
end
respond_to do |format|
format.html # index.html.erb
end
end
# GET /bookings/1
def show
@booking = Booking.find(params[:id])
goto_booking(@booking)
end
# GET /bookings/1/edit
def edit
@booking = Booking.find(params[:id])
@main_heading = 'Edit Booking'
end
# Go to booking
def goto_booking(booking)
site = @booking.site
slot_day = @booking.slot_day
slot_time = @booking.slot_time
# don't need to check slot_time because it is optional
if session[:booking_context] and session[:booking_context] == 'diary' and site and slot_day
# Go the diary page
respond_to do |format|
format.html { redirect_to diary_slot_day_time_path(site, slot_day, slot_time) }
end
else
# Go to booking edit page
respond_to do |format|
format.html { redirect_to edit_booking_path(@booking) }
end
end
end
# POST /bookings
def create
@booking = Booking.new(params[:booking])
unless @booking and @booking.slot_time and @booking.slot_time.number_of_free_slots > 0
flash[:notice] = 'Slot no longer available'
redirect_to :back
return
end
if @booking.save
flash[:notice] = 'Booking was successfully created'
goto_booking(@booking)
else
@main_heading = 'Review New Booking'
respond_to do |format|
format.html { render action: "new" }
end
end
end
# PUT /bookings/1
def update
@booking = Booking.find(params[:id])
if @booking.update_attributes(params[:booking])
flash[:notice] = 'Booking was successfully updated'
goto_booking(@booking)
else
@main_heading = 'Review Booking'
respond_to do |format|
format.html { render action: "edit" }
end
end
end
def confirmation
begin
@booking = Booking.find(params[:booking][:id])
@booking.update_attributes(params[:booking])
if params[:make_unexpected]
@booking.provisional_appointment = nil
@booking.save!
end
@booking.update_slot!
success = true
rescue => e
flash[:error] = e.message
success = false
end
if success
flash[:notice] = 'Booking confirmed'
respond_to do |format|
format.html { redirect_to booking_path(@booking) }
end
else
redirect_to :back
end
end
# DELETE /bookings/1
def destroy
@booking = Booking.find(params[:id])
if @booking
if session[:booking_context] and session[:booking_context] == 'diary'
site = @booking.site
slot_day = @booking.slot_day
slot_time = @booking.slot_time
end
if @booking.destroy
flash[:notice] = 'booking removed'
else
flash[:error] = 'could not remove booking'
end
end
# don't need to check slot_time; that's optional
if site and slot_day
# redirect to diary page
respond_to do |format|
format.html { redirect_to diary_slot_day_time_path(site, slot_day, slot_time) }
end
else
# redirect to booking index
respond_to do |format|
format.html { redirect_to bookings_url }
end
end
end
def diary_slot_day_time_path(*args)
@template.diary_slot_day_time_path(*args)
end
end
class BookingsController0
flash[:注意]=“插槽不再可用”
重定向到:返回
返回
结束
如果@booking.save
flash[:notice]=“预订已成功创建”
goto_预订(@booking)
其他的
@主标题='查看新预订'
回应待办事项|格式|
format.html{呈现操作:“新建”}
结束
结束
结束
#出售/预订/1
def更新
@booking=booking.find(参数[:id])
if@booking.update_属性(参数[:booking])
flash[:notice]=“预订已成功更新”
goto_预订(@booking)
其他的
@主标题='查看预订'
回应待办事项|格式|
format.html{呈现操作:“编辑”}
结束
结束
结束
def确认
开始
@booking=booking.find(参数[:booking][:id])
@预订。更新_属性(参数[:预订])
如果参数[:使_意外]
@预订。临时预约=无
@预订。保存!
结束
@预订。更新您的位置!
成功=正确
救援=>e
闪光[:错误]=e.message
成功=错误
结束
如果成功
flash[:通知]=“预订已确认”
回应待办事项|格式|
format.html{重定向到预订路径(@booking)}
结束
其他的
重定向到:返回
结束
结束
#删除/预订/1
def销毁
@booking=booking.find(参数[:id])
如果@预订
如果会话[:booking\u context]和会话[:booking\u context]=“日记”
site=@booking.site
slot\u day=@booking.slot\u day
slot\u time=@booking.slot\u time
结束
如果@booking.destroy
flash[:notice]=“预订已删除”
其他的
flash[:错误]=“无法删除预订”
结束
结束
#不需要检查插槽时间;那是可选的
如果是站点和插槽_天
#重定向到日记页
回应待办事项|格式|
format.html{重定向到日志(时隙)日期(时间)路径(站点,时隙)日期,时隙)
结束
其他的
#重定向到预订索引
回应待办事项|格式|
format.html{重定向到预订地址}
结束
结束
结束
def日记\时段\日期\时间\路径(*args)
@模板.日记\时段\日期\时间\路径(*args)
结束
结束
我认为这可能与路由错误有关,我可能是错的。这是我的完整跟踪错误:。我已经查看了跟踪,它向我指出我的
转到预订
,创建
方法有问题 为什么要在控制器底部定义此方法
def diary_slot_day_time_path(*args)
@template.diary_slot_day_time_path(*args)
end
这试图引用一个名为@template
的未定义实例变量,这就是为什么会出现该错误
您应该在管线中定义此方法,方法是使用嵌套管线,如下所示:
match '/diary/:day/:time', :to => "some_controller#some_action", :as => "diary_slot_day_time"
原因是is通过模板/视图上下文调用帮助器。但由于某些原因,@template无法工作。通常,控制器无法访问帮助器,因为它们用于视图。所以我现在的问题是,你会把自定义路径方法放在哪里?在库或模型中?不,为什么?你不应该这样做。控制器具有可用的路由帮助程序。