Reputation: 25
I'm fairly new to web development in general and I've been working on a Ruby on Rails app that I want (eventually) to interact with an Android app. I currently am looking for the most appropriate and safe way to create an object from an http POST that would be sent from the Android app.
The way I'm approaching it to initially achieve this by using a REST client extension for chrome trying to send POSTs to my app on localhost and have it create an object from the POST information.
This is the result of the POST from the chrome extension:
Started POST "/problems" for 127.0.0.1 at 2012-09-15 14:16:19 -0400
Processing by ProblemsController#create as */*
Parameters: {"user"=>"7876483097", "latitude"=>"18.378383", "longitude"=>"-67.026201", "ptype"=>"2", "description"=>"Poste roto"}
←\[1m←\[36m (0.0ms)←\[0m ←\[1mbegin transaction←\[0m
←\[1m←\[35m (0.0ms)←\[0m rollback transaction
Rendered problems/new.html.erb within layouts/application (31.0ms)
Rendered layouts/_shim.html.erb (0.0ms)
←\[1m←\[36mUser Load (0.0ms)←\[0m ←\[1mSELECT "users".* FROM "users" WHERE "users"."remember_token" = 'gdmKIurcqDOMoDGWE4IBng' LIMIT 1←\[0m
Rendered layouts/_header.html.erb (4.0ms)
Rendered layouts/_footer.html.erb (1.0ms)
Completed 200 OK in 93ms (Views: 87.0ms | ActiveRecord: 0.0ms)
Right where "rollback transaction" appears means that @problem.save in the create method has failed. Can anybody help identify what I'm missing or how i should change my approach to do it correctly.
Here is my model schema:
create_table "problems", :force => true do |t|
t.string "user"
t.float "latitude"
t.float "longitude"
t.integer "ptype"
t.string "description"
t.integer "priority"
t.integer "status"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "avatar_file_name"
t.string "avatar_content_type"
t.integer "avatar_file_size"
t.datetime "avatar_updated_at"
t.boolean "gmaps"
t.string "address"
end
Here is my create method:
def create
@problem = Problem.new(params[:problem])
if @problem.save
flash[:success] = "Problema guardado"
redirect_to @problem
else
@problem.errors.full_messages
flash.now[:error] = 'Informacion incorrecta'
render 'new'
end
end
This is the POST body:
user=7876483097&latitude=18.378383&longitude=-67.026201&ptype=2&description=Poste+roto
And, this is the problem model and validations
class Problem < ActiveRecord::Base
acts_as_gmappable :latitude => 'latitude', :longitude => 'longitude', :process_geocoding => :geocode?,
:address => "address", :normalized_address => "address",
:msg => "Lo sentimos, ni Google puede localizar esa direccion"
attr_accessible :user, :latitude, :longitude, :ptype, :description, :avatar, :address
validates(:user, presence: true)
validates(:latitude, presence: true)
validates(:longitude, presence: true)
validates(:ptype, presence: true)
Thanks in advance.
Upvotes: 2
Views: 165
Reputation: 662
This is what the post construct should look like:
Parameters: {"problem"=>{"user"=>"7876483097", "latitude"=>"18.378383", "longitude"=>"-67.026201", "ptype"=>"2", "description"=>"Poste roto"}}
instead of what you have:
Parameters: {"user"=>"7876483097", "latitude"=>"18.378383", "longitude"=>"-67.026201", "ptype"=>"2", "description"=>"Poste roto"}
The problem is probably just in the way you're constructing the post with the rest client extension. I find it easier to just use curl through the command line or terminal.
Try using something like this in the terminal or command line:
curl -H "Content-Type:application/json" -H "Accept:application/json" -d "{\"problem\":{\"user\":\"7876483097\", \latitude\":\"18.378383\", \"longitude\":\"-67.026201\",\"ptype\":\"2\",\"description\":\"posteroto\"}}" http://localhost:3000/problems
Upvotes: 0
Reputation: 9003
The problem is with the naming of the fields you send as POST body.
Note that the new instance of Problem
is built using params[:problem]
.
Hence the fields are supposed to be named as problem[user]
, problem[latitute]
, etc. instead of just user
or latitude
.
Upvotes: 2