1
当我得到一个‘试图提出一个嵌套的轨道形式时NoMethodError’。该表单用于创建预留,以及serviceType - 预留(连接模型)。导轨的has_many通过加盟模式未定义ID:“未定义的方法'属性为#”提交嵌套形式
我得到的错误是“未定义的方法'属性为#”
这是我的预订模式: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
这是服务类型的预定模式:servicetypereservation.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
这是保留控制器保留PARAMS:
# 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是否属于该属性。我试着改变一下代码,并得到错误“未定义的属性'0'”,所以我很确定这是id。
当我创建预留(和servicetype-reservation)时,它不会自动生成一个id吗? (它通常在轨道中..)。
任何帮助,将不胜感激。谢谢!