如何在Rails控制器中读取json?

时间:2012-03-06 21:28:57

标签: ruby-on-rails json activesupport

所以,我在控制器操作中这样做:

json_document = JSON(params) (json gem)

如果我打印json_document,它会给我一个类似的Json:

 {"createdAt":"Mar 6, 2012 6:12:54 PM","id":177139718620856322,"text":"@rgjunio where?","source":"web","isTruncated":false,"inReplyToStatusId":177139644012572673, ...

但是当我尝试访问它时:

tweet = Complaint.find_by_twitter_status json_document["inReplyToStatusId"]

我收到以下错误:

can't convert ActiveSupport::HashWithIndifferentAccess into String

所以我尝试了另一种使用符号的方式:

tweet = Complaint.find_by_twitter_status json_document[:inReplyToStatusId]

但它给了我错误:

TypeError (can't convert Symbol into Integer)

有什么想法吗?

更新: 如果我使用params [:inReplyToStatusId],我得到以下内容:

  ActiveRecord::StatementInvalid (PG::Error: ERROR:  operator does not exist: character varying = bigint
 HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
SELECT  "complaints".* FROM "complaints"  WHERE "complaints"."twitter_status" = 177144980450721794 LIMIT 1):

1 个答案:

答案 0 :(得分:1)

尝试:

params[:inReplyToStatusId].to_s