Ruby on Rails - 创建多个对象和关联的一种形式

问题描述 投票:0回答:1

我正在尝试准备一个应用程序,我们可以在其中选择航班和乘客数量。根据乘客数量(params[:_passengers] 和选择的航班(params[:_chosen_flight_id]),我正在准备一个带有表单的视图来收集乘客数据。 enter image description here

它正在创建对 bookings#create 操作的 POST 请求,我尝试使用 booking.build_passenger 方法创建 1 个预订(flight_id,passenger_id) 和关联乘客:

Started POST "/bookings" for 127.0.0.1 at 2024-12-18 17:18:29 +0100
Processing by BookingsController#create as TURBO_STREAM
  Parameters: {"authenticity_token"=>"[FILTERED]", "_passengers"=>"2", "_chosen_flight_id"=>"7", "booking"=>{"0"=>{"passenger_attributes"=>{"passport_no"=>"123123123", "name"=>"Marek", "surname"=>"Surname"}}, "1"=>{"passenger_attributes"=>{"passport_no"=>"123123121", "name"=>"Marek", "surname"=>"Example"}}}, "button"=>""}
  Flight Load (0.2ms)  SELECT "flights".* FROM "flights" WHERE "flights"."id" = $1 LIMIT $2  [["id", 7], ["LIMIT", 1]]
  ↳ app/controllers/bookings_controller.rb:14:in `create'
Unpermitted parameters: :0, :1. Context: { controller: BookingsController, action: create, request: #<ActionDispatch::Request:0x00007f49722664a8>, params: {"authenticity_token"=>"[FILTERED]", "_passengers"=>"2", "_chosen_flight_id"=>"7", "booking"=>{"0"=>{"passenger_attributes"=>{"passport_no"=>"123123123", "name"=>"Marek", "surname"=>"Surname"}}, "1"=>{"passenger_attributes"=>{"passport_no"=>"123123121", "name"=>"Marek", "surname"=>"Example"}}}, "button"=>"",  "controller"=>"bookings", "action"=>"create"} }
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.2ms (1 query, 0 cached) | GC: 0.0ms)



ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError):

代码如下:

#bookings_controller
class BookingsController < ApplicationController
  def new
    @bookings = []
    params[:_passengers].to_i.times do
      @bookings << Booking.new
    end
    @bookings.each do |booking|
      booking.build_passenger
    end
    @flight = Flight.find(params[:_chosen_flight_id])
  end

  def create
    @flight = Flight.find(params[:_chosen_flight_id])

    params[:_passengers].to_i.times do
      i = 0
      @booking = Booking.new
      @booking.flight = @flight
      @booking.build_passenger(params[:booking]["#{i}"][:passenger_attributes])
      i = i + 1
      puts "Booking no#{i}:"
      puts @booking.inspect
    end
  end

  private
  def booking_params
    params.require(:booking).permit(:flight_id, passenger_attributes: [:passport_no, :name, :surname])
  end
end
#bookings/new.html.erb form
<%= form_tag bookings_path do %>
  <%= hidden_field_tag :_passengers, params[:_passengers] %>
  <%= hidden_field_tag :_chosen_flight_id, params[:_chosen_flight_id] %>
  <ul>
  <% @bookings.each_with_index do |booking, i| %>
    Passenger information:
    <%= fields_for "booking[#{i}]", booking do |booking_form| %>
      <%= booking_form.fields_for :passenger do |passenger_form| %>
        <li>
        <%= passenger_form.label :passport_no %>
        <%= passenger_form.text_field :passport_no %>
        <%= passenger_form.label :name %>
        <%= passenger_form.text_field :name %>
        <%= passenger_form.label :surname %>
        <%= passenger_form.text_field :surname %>
        </li>
      <% end %>
    <% end %>
  <% end %>
  <%= button_tag "SUBMIT" %>
  </ul>
<% end %>

有什么想法如何迭代我的参数并创建预订乘客对吗?

ruby-on-rails ruby
1个回答
0
投票

您应该使用名为

booking_params
的对象,而不是使用 params 对象。

class BookingsController < ApplicationController
  def index
    Booking.create
    puts booking_params
  end
  def booking_params
    params.require(:booking)
  end

end
app/controllers/bookings_controller.rb:3:in 'BookingsController#index'
Started GET "/?booking=[foo]" for 127.0.0.1 at 2024-12-18 23:20:44 -0500
Processing by BookingsController#index as HTML
  Parameters: {"booking" => "[foo]"}
  TRANSACTION (0.0ms)  BEGIN immediate TRANSACTION /*action='index',application='Bookings',controller='bookings'*/
  ↳ app/controllers/bookings_controller.rb:3:in 'BookingsController#index'
  Booking Create (1.1ms)  INSERT INTO "bookings" ("created_at", "updated_at") VALUES ('2024-12-19 04:20:44.890479', '2024-12-19 04:20:44.890479') RETURNING "id" /*action='index',application='Bookings',controller='bookings'*/
  ↳ app/controllers/bookings_controller.rb:3:in 'BookingsController#index'
  TRANSACTION (5.6ms)  COMMIT TRANSACTION /*action='index',application='Bookings',controller='bookings'*/
  ↳ app/controllers/bookings_controller.rb:3:in 'BookingsController#index'
[foo]
© www.soinside.com 2019 - 2024. All rights reserved.