Rails形式,遍历集合

我试图遍历表单中的集合,我想请用户为每个Extra_Guest填写数量。

我当前的表单确实按预期显示了集合,但没有将extra_guest_id传递给reservation_extra_guest

模型

class Reservation < ApplicationRecord
  has_many :reservation_extra_guests,dependent: :destroy
  has_many :extra_guests,through: :reservation_extra_guests
  accepts_nested_attributes_for :reservation_extra_guests
end

class ReservationExtraGuest < ApplicationRecord
  belongs_to :extra_guest
  belongs_to :reservation
  accepts_nested_attributes_for :extra_guest
end

class ExtraGuest < ApplicationRecord
  belongs_to :age_table
  has_many :reservation_extra_guests
  has_many :reservations,through: :reservation_extra_guests
end

控制器

def new_second_part
    @hotel = Hotel.find(params[:hotel_id])
    @reservation = Reservation.find(params[:id])
    @guests = []
    @reservation.room.room_category.extra_guests.each do |guest|
      res_guest = @reservation.reservation_extra_guests.build
      res_guest[:extra_guest_id] = guest.id
      @guests << res_guest
    end
end

表格

<%= simple_form_for [@hotel,@reservation] do |f|%>

<%= f.simple_fields_for :reservation_extra_guests do |g| %>
  <p><%= g.object.extra_guest.age_table.name %>
  <%= g.input :extra_guest_quantity,label: false,collection: 1..20 %></p>
  <% end %>

<% end %>

已发送参数

"reservation"=>{"reservation_extra_guests_attributes"=>{"0"=>{"extra_guest_quantity"=>"3"},"1"=>{"extra_guest_quantity"=>"4"}}
redstonegu 回答:Rails形式,遍历集合

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3117630.html

大家都在问