Rails has_many 通过join model undefined id: "undefined method `attributes' for #" 提交嵌套表单时
Rails has_many through join model undefined id: "undefined method `attributes' for #" when submitting nested form
我在尝试提交嵌套的 rails 表单时收到 'NoMethodError'。该表单用于创建预订和服务类型 - 预订(连接模型)。
我得到的错误是 "undefined method `attributes' for #"
这是我的预订模型:reservation.rb
module Booking
class Reservation < ActiveRecord::Base
has_many :service_type_reservations, inverse_of: :reservation, dependent: :destroy
accepts_nested_attributes_for :service_type_reservations, allow_destroy: true
has_many :service_types, through: :service_type_reservations, dependent: :destroy
has_one :customer_reservation, dependent: :destroy
has_one :customer, through: :customer_reservation, dependent: :destroy
validates_uniqueness_of :service_type_reservations
end
end
这是服务类型-预订模型:服务类型reservation.rb
module Booking
class ServiceTypeReservation < ActiveRecord::Base
belongs_to :service_type
belongs_to :reservation
belongs_to :service_calendar
validates :reservation, presence: true
validates :service_type, presence: true
end
end
在我的预订控制器中,这些是新功能和创建功能:reservations_controller.rb
# GET /reservations/new
def new
@serviceTypes = ServiceType.all
@reservation = Reservation.new
@reservation.build_service_type_reservations
@numberOfServices = @serviceTypes.length
if params[:service_type_id]
@selectedService = ServiceType.find(params[:service_type_id])
end
end
# POST /reservations
def create
@serviceTypes = ServiceType.all
@reservation = Reservation.new(reservation_params)
if @reservation.save
redirect_to new_customer_path(reservation_id: @reservation.id), notice: 'Reservation was successfully created.'
else
render :new
end
end
这是预订控制器中的预订参数:
# Only allow a trusted parameter "white list" through.
def reservation_params
respond_to do |format|
format.html { params.require(:reservation).permit(:updated_at, :created_at, :total_price, :customer_id, service_type_reservations_attributes: [:occupancy, :check_in, :check_out, :date, :service_type_id])}
end
end
要创建嵌套表单:
<%= form_for(@reservation) do |f| %>
<%= f.fields_for :service_type_reservations do |service_reservation_form| %>
我很确定它正在创建一个新的服务类型预订,但它有一个未定义的 ID。我不确定 id 是否是那个属性。我已经尝试稍微更改代码并收到错误 "undefined attribute '0'" 所以我很确定它是 id。
当我创建预订(和 servicetype-reservation)时,它不会自动生成一个 ID 吗? (它通常在 rails..)。
如有任何帮助,我们将不胜感激。谢谢!
我删除了那行
validates_uniqueness_of :service_type_reservations
来自 reservations.rb
现在可以提交表单了。之前没有这条线有一个错误,但它突然开始工作了。我想我只需要重新启动服务器。
我在尝试提交嵌套的 rails 表单时收到 'NoMethodError'。该表单用于创建预订和服务类型 - 预订(连接模型)。
我得到的错误是 "undefined method `attributes' for #"
这是我的预订模型:reservation.rb
module Booking
class Reservation < ActiveRecord::Base
has_many :service_type_reservations, inverse_of: :reservation, dependent: :destroy
accepts_nested_attributes_for :service_type_reservations, allow_destroy: true
has_many :service_types, through: :service_type_reservations, dependent: :destroy
has_one :customer_reservation, dependent: :destroy
has_one :customer, through: :customer_reservation, dependent: :destroy
validates_uniqueness_of :service_type_reservations
end
end
这是服务类型-预订模型:服务类型reservation.rb
module Booking
class ServiceTypeReservation < ActiveRecord::Base
belongs_to :service_type
belongs_to :reservation
belongs_to :service_calendar
validates :reservation, presence: true
validates :service_type, presence: true
end
end
在我的预订控制器中,这些是新功能和创建功能:reservations_controller.rb
# GET /reservations/new
def new
@serviceTypes = ServiceType.all
@reservation = Reservation.new
@reservation.build_service_type_reservations
@numberOfServices = @serviceTypes.length
if params[:service_type_id]
@selectedService = ServiceType.find(params[:service_type_id])
end
end
# POST /reservations
def create
@serviceTypes = ServiceType.all
@reservation = Reservation.new(reservation_params)
if @reservation.save
redirect_to new_customer_path(reservation_id: @reservation.id), notice: 'Reservation was successfully created.'
else
render :new
end
end
这是预订控制器中的预订参数:
# Only allow a trusted parameter "white list" through.
def reservation_params
respond_to do |format|
format.html { params.require(:reservation).permit(:updated_at, :created_at, :total_price, :customer_id, service_type_reservations_attributes: [:occupancy, :check_in, :check_out, :date, :service_type_id])}
end
end
要创建嵌套表单:
<%= form_for(@reservation) do |f| %>
<%= f.fields_for :service_type_reservations do |service_reservation_form| %>
我很确定它正在创建一个新的服务类型预订,但它有一个未定义的 ID。我不确定 id 是否是那个属性。我已经尝试稍微更改代码并收到错误 "undefined attribute '0'" 所以我很确定它是 id。
当我创建预订(和 servicetype-reservation)时,它不会自动生成一个 ID 吗? (它通常在 rails..)。
如有任何帮助,我们将不胜感激。谢谢!
我删除了那行
validates_uniqueness_of :service_type_reservations
来自 reservations.rb
现在可以提交表单了。之前没有这条线有一个错误,但它突然开始工作了。我想我只需要重新启动服务器。