簡體   English   中英

如何在 ruby​​ on rails 中的活動記錄關聯的控制器中添加另一個模型的 id

[英]how to add id of another model in the controller on active record association in ruby on rails

我必須制作一個酒店預訂系統,所以我制作了一個用戶、酒店和預訂模型,並給出了所有必要的關聯。 在預訂模型中,我提供了 user 和 hotel:references,因此在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' %>

預訂控制器.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?

Rails 的方法是創建一個嵌套路由

resources :hotels do
  resources :bookings, shallow: true
end

這將創建路由GET /hotels/:hotel_id/bookings/newPOST /hotels/:hotel_id/bookings以及命名幫助器new_hotel_booking_path

<%= link_to 'Book Now', new_hotel_booking_path(@hotel), class: 'btn btn-success btn-sm' %>

調整您的控制器,以便您在回調中創建設置酒店並在酒店外建立預訂:

class BookingsController < ApplicationController
  before_action :set_hotel, only: [:new, :index, :create]

  # GET /hotels/1/bookings/new
  def new
    @booking = @hotel.bookings.new
  end

  # POST /hotels/1/bookings
  def create
    @booking = @hotel.bookings.new(booking_params) do |b|
      b.user = current_user
    end
    respond_to do |format|
      if @booking.save
        format.html { redirect_to @booking, notice: 'Booking successful' }
        format.json { status: :created, location: @booking } # send either a Location header or a response entity - not both
      else
        format.html { render :new }
        format.json { render json: @booking.errors, status: :unprocessable_entity }
      end
    end
  end

  private

  def set_hotel
    @hotel = Hotel.find(params[:hotel_id])
  end 

  # ...
end

傳遞數組以使用表單創建嵌套資源:

# Rails < 5.1
<%= form_for([@hotel, @booking]) do |f| %>

# Rails 5.1+
<%= form_with(model: [@hotel, @booking]) do |f| %>

我相信您有如下booking模式:

class Booking
  belongs_to :user
  belongs_to :hotel
......
end

我假設您在頁面中有一個hotel列表或有hotel對象,並且在您有Book Now鏈接之前。 在代碼中,您已將id作為參數傳遞,但您應該傳遞hotel_id而不是id

<%= link_to 'Book Now', new_booking_path(hotel_id: @hotel.id), :class => 'btn btn-success btn-sm' %>

然后在booking_controller.rb你必須有booking_params方法以及createnew方法。

def new
  @booking = Booking.new
  @booking.hotel = Hotel.find(params[:hotel_id])
end

def create
  @booking = Booking.new(booking_params.merge!(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

private
def booking_params
  params.require(:booking).permit(:user_id, :hotel_id, add_other_fields_you_want_to_save)
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM