[英]ArgumentError (wrong number of arguments (3 for 0..1)):
This is my model: 这是我的模型:
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
Code to call controller action: 调用控制器动作的代码:
$.ajax({
type: "POST",
url: "/create",
data: {
appointment_date: date,
doctor_id: "1",
user_id: "1"
}
// Etc. }); //等});
Action: 行动:
def create
@appointment = Appointment.new(:appointment_date, :doctor_id, :user_id)
if @appointment.save
flash[:success] = "Welcome!"
redirect_to @user
else
alert("failure!")
end
And I got this error: 我得到了这个错误:
ArgumentError (wrong number of arguments (3 for 0..1)):
How to handle this error? 如何处理这个错误? Any advice?
有什么建议吗? Thanks.
谢谢。
Simply passing the symbols to the Appointment constructor doesn't work. 仅将符号传递给约会构造函数是行不通的。
You need to pass the variables like this: 您需要像这样传递变量:
Appointment.new(:appointment_date => params[:appointment_date], ...)
Since the params hash already contains all these values, you can pass these in directly like zwippie already showed. 由于params哈希值已经包含所有这些值,因此您可以像已经显示的zwippie一样直接传递这些值。
Just a headsup for the future: in case your params hash contains values you don't want to pass to the constructor, use Appointment.new(params.except(:unwanted_key))
只是对未来的警告:如果您的params哈希包含不想传递给构造函数的值,请使用
Appointment.new(params.except(:unwanted_key))
如果尝试:
@appointment = Appointment.new(params[:data])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.