Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to Rails with a JSON API

I've been trying to write to a regular one model rails app from POSTman for a few days now, and can't figure out how to do it, or find any information on how to do it.

My app 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 a basic resource on how to do it.

I imagine this is pretty basic.

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 happening, but the record isn't actually being updated. Do I need to change something in the controller? It's just a regular rails generated controller with no changes.

EDIT 3:

Here is my output from just 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've changed nothing from the scaffold, and I haven't been able to figure out how to format the JSON to nest it under user like 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 don't understand why this is so difficult. I'm just trying to update a name via JSON ...

like image 302
Arel Avatar asked Jul 17 '13 19:07

Arel


Video Answer


1 Answers

For the controller you want to post to:

protect_from_forgery with: :null_session

or like from: How do I bypass protect_from_forgery in Rails 3 for a Facebook canvas app?

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

EDIT

Your JSON is incorrect... you are posting

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

Since your controller does user.update_attribute(params[:user]), your JSON needs to be under a user attribute

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

This will create a hash of

{"user"=>{"name"=>"Jeff"}, "id"=>"1"}
like image 89
Jesse Wolgamott Avatar answered Nov 09 '22 16:11

Jesse Wolgamott