Writing to Rails with the JSON API

I have been trying to write in a regular single application for rails models from POSTman for several days, and cannot figure out how to do this, or find any information on how to do this.

My application has a user model with a name field. All I'm trying to do is change the name remotely via JSON using POSTman.

Any help on how to do this is appreciated, including a link to the main resource on how to do this.

I guess this is pretty simple.

EDIT: here is a screenshot from the POSTman output

enter image description here

EDIT 2:

from server log:

Started PUT "/users/1.json" for 127.0.0.1 at 2013-07-17 16:53:36 -0400 Processing by UsersController#update as JSON Parameters: {"name"=>"Jeff", "id"=>"1"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", "1"]] (0.1ms) begin transaction (0.1ms) commit transaction Completed 204 No Content in 3ms (ActiveRecord: 0.4ms) 

The transaction is in progress, but the record is not really being updated. Do I need to change something in the controller? This is just an ordinary rail, created by the controller without changes.

EDIT 3:

Here is my conclusion from going to http://localhost:3000/users/1.json

 { "created_at": "2013-07-02T21:51:22Z", "id": 1, "name": "Arel", "updated_at": "2013-07-02T21:51:22Z" } 

Again, I didn’t change anything from the scaffold, and I could not figure out how to format the JSON to insert it under the user, as the answer suggests.

Here is the relevant part of my controller:

 # GET /users/1 # GET /users/1.json def show @user = User.find(params[:id]) respond_to do |format| format.html # show.html.erb format.json { render json: @user } end end # PUT /users/1 # PUT /users/1.json def update @user = User.find(params[:id]) respond_to do |format| if @user.update_attributes(params[:user]) format.html { redirect_to @user, notice: 'User was successfully updated.' } format.json { } else format.html { render action: "edit" } format.json { render json: @user.errors, status: :unprocessable_entity } end end end 

I do not understand why it is so difficult. I'm just trying to update the name via JSON ...

+8
json api ruby-on-rails
source share
1 answer

For the controller to which you want to send a message:

 protect_from_forgery with: :null_session 

or how from: How to get around the_from_forgery protection in Rails 3 for a Facebook canvas application?

 skip_before_filter :verify_authenticity_token, :only => [THE ACTION] 

EDIT

Your JSON is incorrect ... you are posting

 {"name"=>"Jeff", "id"=>"1"} 

Since your controller has user.update_attribute(params[:user]) , your JSON must be under the user attribute

 { "id": 1, "user": { "name": "Jeff" } } 

This will create a hash

 {"user"=>{"name"=>"Jeff"}, "id"=>"1"} 
+5
source share

All Articles