1

这是我的模型:

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"
  }

// ETC。 });

行动:

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)

我正在使用 FullCalendar 插件,它以这种格式获取日期:

Fri Sep 13 2013 12:00:50 GMT-0400 (EDT)

我将该日期保存到日期时间字段中的数据库中。

有任何想法吗?

4

2 回答 2

1

如果它是约会控制器的创建操作,那么它将是

$.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
于 2013-09-13T05:11:02.573 回答
1

当您进行 ajax 调用时,您可以访问变量,例如

params[:appointment_date]
params[:doctor_id]
params[:user_id]

并不是

params[:data][:appointment_date]
params[:data][:doctor_id]
params[:data][:user_id]

由于没有所谓的 params[:data] 它给了你一个 nil 类错误。

于 2013-09-13T04:34:35.530 回答