【发布时间】:2011-11-28 05:27:57
【问题描述】:
我正在尝试通过 iPhone 应用程序中的 json 在我的 rails 后端创建一个用户帐户。以下是当前发布到服务器的内容:
Started POST "/patients" for 127.0.0.1 at 2011-11-27 20:52:29 -0800
Processing by PatientsController#create as HTML
Parameters: {"patient"=>"{\"password\":\"password\",\"password_confirmation\":\"password\",\"email\":\"testagain\"}"}
WARNING: Can't verify CSRF token authenticity
Completed 500 Internal Server Error in 1ms
NoMethodError (undefined method `stringify_keys' for #<String:0x00000104a354f8>):
app/controllers/patients_controller.rb:43:in `new'
app/controllers/patients_controller.rb:43:in `create'
通过直接从浏览器发布这些是提交的参数:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"H2iYdzdfokQs91AAozb+taMTdV2y5xLRaCni5XKQN4w=", "patient"=>{"email"=>"test", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create Patient"}
从我在其他地方读到的内容来看,stringify_keys 意味着这些操作需要一个哈希(我认为我几乎重建了),因为我正在使用这段代码来创建一个新用户:
@patient = Patient.new(params[:patient])
我还认为,如果我使用 JSON 格式发帖,真实性令牌并不重要……重要吗?
所有问题:这是从 iphone 应用程序发布到 rails 后端的正确方法吗?重新创建参数哈希?将不胜感激任何朝着正确方向的推动。
为了完整起见,这里是我用来从我的 iOS 应用发布的代码 sn-p:
NSDictionary *json = [self createSignUpDictionary];
NSURL *url = [NSURL URLWithString:@"http://localhost:3000/patients"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request addRequestHeader:@"Accepts" value:@"application/json"];
[request setPostValue:[json JSONString] forKey:@"patient"];
[request startAsynchronous];
【问题讨论】:
标签: iphone ruby-on-rails json post asihttprequest