1

I am using react to perform basic requests to rails. So far I have success with GET, POST, and DELETE, but for some reason I can't get PUT to work. I could not find a good resources on fetch - as far as I know from this SO post, it should work similarly with POST.

The address I am trying to send the request to is /api/schedules/9 (I am hardwiring it so it always updates to schedules with ID 9 for now. I can confirm that api/schedules/9 exists). I am including my postSchedule method that works, for comparison.

function postSchedule(date, cb) {
  return fetch(`api/schedules`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      date: date,
      user_id: 1
    })
  }).then((response) => response.json())
    .then(cb);
};

function updateSchedule(scheduleId, date, cb) {
  return fetch(`api/schedules/${scheduleId}`, {
    method: 'PATCH',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      date: date,
      user_id: 1
    })
  }).then((response) => response.json())
    .then(cb);
};

//schedule.js

  postSchedule(){
    let date = this.state.date;
    Client.postSchedule(date, (schedule) => {
      this.setState({schedules: this.state.schedules.concat([schedule])})
    })
  };
  updateSchedule(){
    Client.updateSchedule(9, this.state.date, () => {
      this.setState({schedules: this.state.schedules})
    })
  };
...
    //creating new schedule:
    <input type="date" onChange={this.props.handleDate} />
    <button type="button" onClick={this.props.postSchedule}>Submit New</button>

    //updating schedule:
    <input type="date" onChange={this.props.handleDate} />
    <button type="button" onClick={this.props.updateSchedule}>Submit Change</button>

Inside rails controller:

def create
    @schedule = Schedule.new(schedule_params)
    if @schedule.save
      render json: @schedule
    else
      render json: @schedule, status: :unprocessable_entity
    end
  end

  def update
    @schedule = Schedule.find(params[:id])
    if @schedule.update_attributes
      render json: @schedule
    else
      render json: @schedule, status: :unprocessable_entity
    end
  end

Error:

Started PUT "/api/schedules/9" for 127.0.0.1 at 2017-05-03 17:55:12 -0700
Processing by SchedulesController#update as */*
Parameters: {"date"=>"2017-05-15", "user_id"=>1, "id"=>"9", "schedule"=>{"date"=>"2017-05-15", "user_id"=>1}}
Schedule Load (0.8ms)  SELECT  "schedules".* FROM "schedules" WHERE "schedules"."id" = ? LIMIT ?  [["id", 9], ["LIMIT", 1]]
Completed 500 Internal Server Error in 13ms (ActiveRecord: 0.8ms)


ArgumentError (wrong number of arguments (0 for 1)):

app/controllers/schedules_controller.rb:23:in `update'

This is what ...api/schedules/9 looks like:

{
"id": 9,
"date": "2017-05-20",
"created_at": "2017-05-03T23:41:33.750Z",
"updated_at": "2017-05-04T00:57:58.260Z",
"user_id": 1
}

Why is it showing argument error? What argument did I fail to provide?

Community
  • 1
  • 1
Iggy
  • 5,129
  • 12
  • 53
  • 87

1 Answers1

1

You are simply missing the first argument for update_attributes. It should look like this:

def update
  @schedule = Schedule.find(params[:id])
  if @schedule.update_attributes(schedule_params)
    render json: @schedule
  else
    render json: @schedule, status: :unprocessable_entity
  end
end

Notice this: @schedule.update_attributes(schedule_params)

Ken Stipek
  • 1,512
  • 8
  • 12