Writing to Rails with a JSON API -
i've been trying write regular 1 model rails app postman few days now, , can't figure out how it, or find information on how it.
my app has user model name field. i'm trying change name remotely via json using postman.
any on how appreciated, including link basic resource on how it.
i imagine pretty basic.
edit: here screenshot postman output
edit 2:
from server log:
started put "/users/1.json" 127.0.0.1 @ 2013-07-17 16:53:36 -0400 processing userscontroller#update json parameters: {"name"=>"jeff", "id"=>"1"} user load (0.2ms) select "users".* "users" "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 happening, record isn't being updated. need change in controller? it's regular rails generated controller no changes.
edit 3:
here output going 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 scaffold, , haven't been able figure out how format json nest under user answer suggests.
here relevant part of controller:
# /users/1 # /users/1.json def show @user = user.find(params[:id]) respond_to |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 |format| if @user.update_attributes(params[:user]) format.html { redirect_to @user, notice: 'user 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 difficult. i'm trying update name via json ...
for controller want post to:
protect_from_forgery with: :null_session
or from: how bypass protect_from_forgery in rails 3 facebook canvas app?
skip_before_filter :verify_authenticity_token, :only => [the action]
edit
your json incorrect... posting
{"name"=>"jeff", "id"=>"1"}
since controller user.update_attribute(params[:user])
, json needs under user attribute
{ "id": 1, "user": { "name": "jeff" } }
this create hash of
{"user"=>{"name"=>"jeff"}, "id"=>"1"}
Comments
Post a Comment