Reputation: 3199
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.
Upvotes: 0
Views: 3511
Reputation: 2716
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.
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))
Upvotes: 4