【发布时间】:2020-09-08 15:08:10
【问题描述】:
我必须创建一个酒店预订系统,所以我创建了一个用户、酒店和预订模型,并给出了所有必要的关联。在我给用户和酒店的预订模型中:参考 因此,在 booking_controller 的 create 方法中创建新预订时,我已经给出了设计 gem 提供的 @booking.user = current_user 。 如何在控制器中提供hotel_id。
这是 schema.rb
create_table "bookings", force: :cascade do |t|
t.integer "user_id", null: false
t.string "guest_name"
t.integer "no_of_guest"
t.integer "room"
t.date "check_in_date"
t.date "check_out_date"
t.integer "hotels_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["hotels_id"], name: "index_bookings_on_hotels_id"
t.index ["user_id"], name: "index_bookings_on_user_id"
end
这是创建预订表格的链接
<%= link_to 'Book Now', new_booking_path(:id => @hotel.id), :class => 'btn btn-success btn-sm' %>
booking_controller.rb
def create
@booking = Booking.new(booking_params)
@booking.user = current_user
respond_to do |format|
if @booking.save
format.html { redirect_to @booking, notice: 'Booking successfully !!!' }
format.json { render :show, status: :created, location: @booking }
else
format.html { render :new }
format.json { render json: @booking.errors, status: :unprocessable_entity }
end
end
end
我应该如何以及在哪里为预订设置特定的酒店 ID?
【问题讨论】:
标签: ruby-on-rails ruby activerecord associations