Manually Generate URL for Rails App (for the create action)
I'm working on an iPhone app and I need t开发者_如何学编程o figure out how to properly nest the information in the URL (really a POST body) in order for rails to recognize it as an object hash such as:
Parameters: {:student => {:name => "Bob", :age => "13"}, :user_credentials=>"..."}
I've tried the following two ways and neither work for me:
/student?user_credentials="..."&student={name="bob"&age="13"}
/student?user_credentials="..."&student=[name="bob"&age="13"]
Both of which basically set the Parameters to {student=>"{name=", age=>"13]", :user_credentials=>"..."}
The code works as far as getting the information there in a POST body, I'm just trying to figure out how to format the string properly. I hope that is clear enough.
The correct way to urlencode input for rails to recognize it as a hash would be
student[name]=bob&student[age]=13
This can be nested many levels deep:
student[location][city]=New%20York
would represent
"student" => { "location" => { "city" => "New York" } }
Having written a few rails apps that talk to iphone apps, I would also recommend against this, and instead communicate by posting data in json or xml format. Teaching your iphone app to 'speak' rails parameter encoding seems like a Bad Idea.
Take a look at Objective Resource a a pretty complete implementation of using REST-like calls using JSON or xml to pass data back and forth: http://iphoneonrails.com/
精彩评论