将JSON从iOS发布到Rails 3.1(Stringify_keys错误) [英] Posting JSON from iOS to Rails 3.1 (Stringify_keys error)

查看:108
本文介绍了将JSON从iOS发布到Rails 3.1(Stringify_keys错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过iPhone应用程序中的json在我的Rails后端上创建用户帐户.这是当前正在发布到服务器的内容:

I'm trying to create a user acount on my rails backend via json from an iPhone app. Here is what is currently being posted to the server:

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'

通过直接从浏览器中发布,这些是提交的参数:

By posting straight from the browser these are the paramaters that are submitted:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"H2iYdzdfokQs91AAozb+taMTdV2y5xLRaCni5XKQN4w=", "patient"=>{"email"=>"test", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Create Patient"}

从我在其他地方阅读过的内容来看,stringify_keys表示操作期望哈希值(我认为我几乎重构了该哈希值),因为我正在使用以下代码创建新用户:

From what I have read elsewhere the stringify_keys means that the actions expects a hash (which I thought I almost reconstructed) since I am using this code to create a new user:

@patient = Patient.new(params[:patient])

我还相信,如果我使用JSON格式进行发布,那么真实性令牌并不重要...这有关系吗?

I also believe that the authenticity token doesn't matter if I'm posting using JSON format... does it matter?

总的来说,这是从iPhone应用程序发布到Rails后端的正确方法吗?重新创建参数哈希?希望您能向正确的方向轻按.

Over all question: Is this the right approach to be posting to a rails backend from an iphone app? Recreating the parameters hash? Would appreciate any nudges in the right direction.

为了完整起见,这是我用来从iOS应用发布的代码段:

For completeness sake here is the code snippet I'm using to post from my iOS app:

 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];

推荐答案

您很可能必须为操作禁用身份验证令牌验证.

You will most likely have to disable the authentication token verification for your action.

只需将以下行放入您的控制器中,一切就可以正常工作.

Just put the following line in your controller and everything should work.

protect_from_forgery :except => :index

但是,如果这样做,请确保对#create函数具有某种形式的自定义保护.您可以阅读以下内容以获取更多信息: http://api.rubyonrails.org/classes /ActionController/RequestForgeryProtection/ClassMethods.html

However if you do that make sure that you have some form of custom protection on your #create function. You can read this for more info: http://api.rubyonrails.org/classes/ActionController/RequestForgeryProtection/ClassMethods.html

这篇关于将JSON从iOS发布到Rails 3.1(Stringify_keys错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆