[英]Rails 5: API no implicit conversion of string into integer
I'm trying to pass data from the Typeform Responses API. 我正在尝试从Typeform Responses API传递数据。 Everything is working except the user generated answers. 除了用户生成的答案之外,一切正常。 Only one answer is working with type: website
the other one type: multiple_choice
for example isn't working. 只有一个答案是使用type: website
另一种type: multiple_choice
例如type: multiple_choice
不起作用。
An example response from Typeform looks like this: Typeform的示例响应如下所示:
"items": [
{
"landing_id": XXXXXXX
},
"answers": [
{
"field": {
"id": "hVONkQcnSNRj",
"type": "website",
"ref": "my_custom_website_reference"
},
"type": "url",
"text": "https://xxxxx.com"
},
{
"field": {
"id": "k6TP9oLGgHjl",
"type": "multiple_choice",
"ref": "my_custom_multiple_choice2_reference"
},
"type": "choice",
"choice": {
"label": "Tokyo"
}
}
]
My OrdersController.rb
我的OrdersController.rb
response = HTTParty.get("https://api.typeform.com/forms/XXXXXX/responses?page_size=25&query=#{current_user.hash_id}",
headers: {"Authorization" => "Bearer #{@token}"})
if response.code.to_i == 200
items = response.parsed_response["items"]
items.each do |item|
@order = current_user.orders.find_or_create_by(landing_id: item["landing_id"]) do |order|
item["answers"].each do |answer|
order.landing_id = item["landing_id"]
order.email = item["hidden"]["email"]
order.price = item["hidden"]["price"]
order.moduls = item["hidden"]["moduls"]
order.project = item["hidden"]["project"]
# This is working
order.website = answer["url"]
# This isn't working
order.payment = answer["choice"]
end
end
end
When I change answer["choice"]
to answer
, I only receive the first field
of the ànswers
, which is https://xxxxx.com
. 当我更改answer["choice"]
来answer
,我只收到了ànswers
的第一个field
,即https://xxxxx.com
。 If I change answer["choice"]
to item["answers]
, I get every field
from the answers
. 如果我将answer["choice"]
更改为item["answers]
,我会从answers
获得每个field
。
If I use item["answers]["field"]["choice"]
or something similar I get this error no implicit conversion of string into integer
. 如果我使用item["answers]["field"]["choice"]
或类似的东西我得到此错误no implicit conversion of string into integer
。
I found the following on the Typeform documentation: 我在Typeform文档中找到了以下内容:
FIELD WITH ANSWER VALUE (RELATIVE TO RESPONSE ITEM OBJECT) 具有响应值的字段(与响应项目对象相关)
answers[n].choice.label
Probably I need to adjust my code? 可能我需要调整我的代码? For me it's really unclear what this means. 对我来说,这真的不清楚这意味着什么。
I'm not sure how answer['url']
works since there's no "url" key on the answers. 我不确定answer['url']
是如何工作的,因为答案上没有“url”键。
Sounds like you want something like... 听起来像你想要的东西......
@order = current_user.orders.find_or_create_by(landing_id: item["landing_id"]) do |order|
order.landing_id = item["landing_id"]
order.email = item["hidden"]["email"]
order.price = item["hidden"]["price"]
order.moduls = item["hidden"]["moduls"]
order.project = item["hidden"]["project"]
website_field = item['answers'].select{|k,v| v['field']['type'] == 'website'}.first
# you have an array of fields, select the first with type "website"
order.website = website_field['text'] # now get the "text" value which is the url
choice_field = item['answers'].select{|k,v| v['field']['type'] == 'multiple_choice'}.first
# same but with the field with type "multiple_choice"
order.payment = choice_field['choice']['label']
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.