Rails 4 Update Active Record with JSON data from AJAX -
i've spent time looking around there not seem answer question. appreciated.
i'm sending json data ajax call rails app (no cors).
javascript:
$.ajax({ type: "post", url: "/tasks/", data: json.stringify(res), datatype: "json", async: true });
the json data (res) copied browser console:
[{"task_id":"9","grid_position":[0,2,2,2,false]},{"task_id":"8","grid_position":[0,0,2,2,false]}]:
so far good.
this gets sent via route:
resources :tasks, only: [:index, :update, :create, :destroy]
to tasks controller, trying reach update action:
def update @task = task.find(params[:id]) respond_to :html, :json if @task.update_attributes(task_params) flash[:success] = "task updated" redirect_to @user else flash[:error] = "task not saved! please see guidance form labels" redirect_to user_url(current_user) end end
i cannot access update action in tasks controller. have tried changing url, changing post put, , i'm stumped.
the server error message:
started post "/tasks" 217.137.84.197 @ 2015-08-19 15:25:25 +0000 processing taskscontroller#create */* parameters: {"{\"task_id\":\"9\",\"grid_position\":"=>{"0,2,2,2,false"=>{"},{\"task_id\":\"8\",\"grid_position\":"=>{"0,0,2,2,false"=>{"}"=>nil}}}}} user load (0.1ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 1]] completed 400 bad request in 4ms (activerecord: 0.1ms)
i can understand it's looking users trying create new task, must have user id. however, wanting update existing task.
$.ajax({ type: "post"
triggers create action.
if want update task, use
type: "patch"
Comments
Post a Comment