这是我的模型:
class User < ActiveRecord::Base
has_many :appointments
has_many :doctors, :through => :appointments
end
class Doctor < ActiveRecord::Base
has_many :appointments
has_many :users, :through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :doctor
belongs_to :user
end
调用控制器操作的代码:
$.ajax({
type: "POST",
url: "/create",
data: {
appointment_date: date,
doctor_id: "1",
user_id: "1"
}
//等 });
行动:
def create
@appointment = Appointment.new(:appointment_date => params[:data][:appointment_date],
:hairdresser_id => params[:data][:doctor_id], :user_id => params[:data][:user_id])
if @appointment.save
flash[:success] = "Welcome!"
redirect_to @user
else
alert("failure!")
end
end
我得到了这个错误:
NoMethodError (undefined method `[]' for nil:NilClass)
我正在使用全日历插件,它以这种格式获取日期:
Fri Sep 13 2013 12:00:50 GMT-0400 (EDT)
我正在将该日期保存到日期时间字段中的数据库中。
有什么想法吗?
如果是约会控制器的创建操作,那么它将是
$.ajax({
type: "POST",
url: "/appointments",
data: {
appointment_date: date,
doctor_id: "1",
user_id: "1"
}
在创建操作中
def create
@appointment = Appointment.new({:appointment_date => params['appointment_date'],
:hairdresser_id => params['doctor_id'], :user_id => params['user_id]'})
if @appointment.save
#YOUR CODE
else
#YOUR CODE
end
end
当你进行和 ajax 调用时,你可以访问变量,如
params[:appointment_date]
params[:doctor_id]
params[:user_id]
而不是
params[:data][:appointment_date]
params[:data][:doctor_id]
params[:data][:user_id]
由于没有所谓的参数[:d ata],它给了你一个零类错误。