Ruby on rails Rails 4-BookingsController中的NoMethodError#使用嵌套资源时创建
我正在使用RubyonRails 4.1为我的示例项目创建一个机票预订应用程序。三是三种模式——活动、门票和预订。活动有许多门票和预订。门票有很多预订,属于活动。预订属于活动和门票 下面是嵌套管线的外观:Ruby on rails Rails 4-BookingsController中的NoMethodError#使用嵌套资源时创建,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,我正在使用RubyonRails 4.1为我的示例项目创建一个机票预订应用程序。三是三种模式——活动、门票和预订。活动有许多门票和预订。门票有很多预订,属于活动。预订属于活动和门票 下面是嵌套管线的外观: resources :events do resources :tickets resources :bookings end 票务控制器工作正常,以下是控制器代码: class TicketsController < ApplicationController def ind
resources :events do
resources :tickets
resources :bookings
end
票务控制器工作正常,以下是控制器代码:
class TicketsController < ApplicationController
def index
@event = Event.find(params[:event_id])
@tickets = @event.tickets.all
end
def show
@event = Event.find(params[:event_id])
@ticket = @event.tickets.find(params[:id])
end
def new
@event = Event.find(params[:event_id])
@ticket = Ticket.new
end
def create
@event = Event.find(params[:event_id])
@ticket = @event.tickets.create(ticket_params)
if @ticket.save
redirect_to [@event, @ticket]
else
render 'new'
end
end
def edit
@event = Event.find(params[:event_id])
@ticket= @event.tickets.find(params[:id])
end
def update
@event = Event.find(params[:event_id])
@ticket = @event.tickets.find(params[:id])
if @ticket.update(ticket_params)
redirect_to [@event, @ticket]
else
render 'edit'
end
end
def destroy
@event = Event.find(params[:event_id])
@ticket = @event.tickets.find(params[:id])
@ticket.destroy
redirect_to event_tickets_path
end
private
def ticket_params
params.require(:ticket).permit(:ticket_name, :booking_start_date, :booking_end_date, :ticket_price, :ticket_quantity, :minimum_quantity, :maximum_quantity, :terms_conditions, :more_information)
end
class ticketcontroller
结束
但是,当我按照步骤创建预订控制器时,会收到一条错误消息:
NoMethodError in BookingsController#create
undefined method `bookings' for #<Event:0x007f84dd6cc7a8>
BookingController中的命名错误#创建
未定义的方法“预订”#
预订控制器如下所示:
class BookingsController < ApplicationController
def new
@event = Event.find(params[:event_id])
@booking = Booking.new
end
def create
@event = Event.find(params[:event_id])
@booking = @event.bookings.create(booking_params)
if @booking.save
redirect_to [@event, @booking]
else
render 'new'
end
end
def show
@event = Event.find(params[:event_id])
@booking = @event.bookings.find(params[:id])
end
private
def booking_params
params.require(:booking).permit(:buyer_name, :email, :mobile, :address, :order_quantity)
end
end
class BookingsController
有人能告诉我我做错了什么吗 我认为您忘了声明活动和预订之间的关系,在活动方面:确保您有以下
多个:
class Event < ActiveRecord::Base
has_many :bookings
# etc.
class事件
您是否在活动
模型中定义了有多少:预订
?非常感谢!我错过了那个。