简体   繁体   English

ArgumentError(参数数量错误(0..1为3)):

[英]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.

相关问题 Geocoder:ArgumentError:参数数目错误(0..1为3) - Geocoder : ArgumentError: wrong number of arguments (3 for 0..1) ArgumentError(arguments 的编号错误(给定 2,预期 0..1) - ArgumentError (wrong number of arguments (given 2, expected 0..1) Devise中的ArgumentError :: RegistrationsController#新错误的参数个数(2个为0..1) - ArgumentError in Devise::RegistrationsController#new wrong number of arguments (2 for 0..1) Rails db:种子“rake aborted! ArgumentError:错误的参数数量(2为0..1)“ - Rails db:seed “rake aborted! ArgumentError: wrong number of arguments (2 for 0..1)” Ruby ArgumentError:arguments 的错误编号(给定 2,预期 0..1) - Ruby ArgumentError: wrong number of arguments (given 2, expected 0..1) ArgumentError-参数数量错误(给定2个,预期值为0..1):排序问题Rails 4.2 - ArgumentError - wrong number of arguments (given 2, expected 0..1): sorting issue Rails 4.2 错误的数字参数(0表示1)(ArgumentError) - wrong number arguments (0 for 1) (ArgumentError) ArgumentError:参数数量错误(0表示1) - ArgumentError: wrong number of arguments (0 for 1) ArgumentError输入错误的参数数目/错误的数目(0表示2) - ArgumentError at / wrong number of arguments (0 for 2) ArgumentError:参数数量错误 - ArgumentError: wrong number of arguments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM