尝试提交嵌套的rails表单时,我收到了'NoMethodError'。表单用于创建预留,以及serviceType - 预留(连接模型)。
我得到的错误是#“
的”未定义方法`属性'这是我的预订模式: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
这是servicetype-reservation模型: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
这是预订控制器中的预订参数:
# 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? (它通常在铁轨上......)。
任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
我删除了该行
var users = [{
name: 'talon',
email: 'talon@test.com',
password: 'test1234',
username: 'tdog',
}, {
name: 'lauren',
email: 'lauren@test.com',
password: 'admin1234',
username: 'ldogg',
}, {
name: 'cohen',
email: 'cohen@test.com',
password: 'baby1234',
username: 'cdoggy',
}, {
name: 'kyler',
email: 'kyler@test.com',
password: 'delete1234',
username: 'kdawg'
}];
var len = users.length;
while (len--) {
if (users[len].email == 'kyler@test.com') {
users.splice(len, 1);
break;
}
}
console.log(users);
来自reservations.rb
现在可以提交表格。没有这条线之前有一个错误,但它突然开始工作。我想我只需要重启服务器。