How do I utilize REST to post GPS data from an Android device into a Ruby on Rails application?

Posted by joecan on Stack Overflow See other posts from Stack Overflow or by joecan
Published on 2010-04-14T07:57:27Z Indexed on 2010/04/14 8:03 UTC
Read the original article Hit count: 327

Filed under:
|
|
|
|

I am a student in the process a building an Android app that can post a GPS track into a Rails application. I would like to do things the "Rails" way and take advantage of the REST. My rails application basically has 3 models at this point: users, tracks, and points. A user has_many tracks and a track has_many points. A track also has a total distance. Points have a latitude and longitude. I have successfully been able to create an empty track with:

curl -i -X POST -H 'Content-Type: application/xml' -d '<track><distance>100</distance></track>' http://localhost:3000/users/1/tracks

Whoo hoo! That is pretty cool. I am really impressed that rails do this. Just to see what would happen I tried the following:

curl -i -X POST -H 'Content-Type: application/xml -d '<track><distance>100</distance><points><point><lat>3</lat><lng>2</lng></point></points></track>' http://localhost:3000/users/1/tracks

Fail! The server spits back:

Processing TracksController#create (for 127.0.0.1 at 2010-04-14 00:03:25) [POST] Parameters: {"track"=>{"points"=>{"point"=>{"lng"=>"2", "lat"=>"3"}}, "distance"=>"100"}, "user_id"=>"1"} User Load (0.6ms) SELECT * FROM "users" WHERE ("users"."id" = 1)

ActiveRecord::AssociationTypeMismatch (Point(#-620976268) expected, got Array(#-607740138)): app/controllers/tracks_controller.rb:47:in `create'

It seems my tracks_controller doesn't like or understand what it's getting from the params object in my tracks_controller.rb:

def create
    @track = @user.tracks.build(params[:track])

My xml might be wrong, but at least Rails seems to be expecting a Point from it. Is there anyway I can fix TracksController.create so that it will be able to parse xml of a track with nested multiple points? Or is there another way I should be doing this entirely?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about android