0

Hi everyone i am making an app with a backend in rails, i need to create a user from an ajax request and the server have to return a json object with the new user saved.

This is my code of rails

class UsersController < ApplicationController

   def nuevo_usuario
      @u = User.new(params[:user])
      @u.save       
      respond_to do |format|
          format.json { render :json => @u }
      end
    end

end

And this is the code of javascript

$.post("http://0.0.0.0:3000/users/nuevo_usuario.json", $("#resgistro-form").serialize(), function(data){});

And the server respond with this

Started POST "/users/nuevo_usuario.json" for 127.0.0.1 at 2013-06-14 10:17:22 -0500
Processing by UsersController#nuevo_usuario as JSON
  Parameters: {"user"=>{"user"=>"prueba", "mail"=>"prueba@gmail.com", "password"=>"[FILTERED]"}}
WARNING: Can't verify CSRF token authenticity
   (0.5ms)  BEGIN
  SQL (72.1ms)  INSERT INTO "users" ("created_at", "mail", "password", "updated_at", "user") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["created_at", Fri, 14 Jun 2013 15:17:22    UTC +00:00], ["mail", "prueba@gmail.com"], ["password", "preuba"], ["updated_at", Fri, 14 Jun 2013 15:17:22 UTC +00:00], ["user", "prueba"]]
   (18.2ms)  COMMIT
Completed 200 OK in 395ms (Views: 1.1ms | ActiveRecord: 310.0ms)

I don't receive the json object that i send with rails.

if you need more information just tell me, thank you :)

kentverger
  • 475
  • 1
  • 5
  • 19

3 Answers3

0

Are you looking at only the log file? That won't include the actual response body.

Andy Waite
  • 10,785
  • 4
  • 33
  • 47
  • I look for the response body with firebug and is empty, i start to think that is something about the crossdomain ajax request – kentverger Jun 14 '13 at 15:40
0

Add "respond_to :json"

So that it is something like:

class UsersController < ApplicationController
       respond_to :json

   def nuevo_usuario
      @u = User.new(params[:user])
      @u.save       
      respond_to do |format|
          format.json { render :json => @u.as_json }
      end
    end

end
Stpn
  • 6,202
  • 7
  • 47
  • 94
  • i add respond_to :json before the nuevo_usuario method and didn't work, i also try with removing respond_to code block and replace it with respond_with function that i found here http://stackoverflow.com/questions/8200929/how-to-convert-this-respond-to-options-to-use-rails-3-version but dont work :( – kentverger Jun 14 '13 at 15:58
  • sorry, I saw I made a typo. – Stpn Jun 14 '13 at 17:19
0

I upload my code to heroku and complied the app with phongap and it works fine, i think that the problem was crossdomain and crossbrowser ajax request

kentverger
  • 475
  • 1
  • 5
  • 19