[英]How to create array for params hash
In my rails controller I'm getting JSON posted to it that contains a person's work history. 在我的rails控制器中,我正在向其中发布JSON,其中包含一个人的工作历史记录。 So they could have multiple jobs.
因此,他们可以有多个工作。 The jobs are in a format like...
作业的格式如...
"values":
[
{
"title": "dummy position",
"company": "company 1",
"location": "Indianapolis, IN",
"description": "dummy job description",
"startDateMonth": "05",
"startDateYear": "2015",
"endDateMonth": "06",
"endDateYear": "2015"
},
{
"title": "dummy position 2",
"company": "company 2",
"location": "Indianapolis, IN",
"description": "dummy job description 2",
"startDateMonth": "02",
"startDateYear": "2015",
"endDateMonth": "05",
"endDateYear": "2015"
},
{
"title": "dummy position 3",
"company": "company 3",
"location": "Indianapolis, IN",
"description": "dummy job description 3",
"startDateMonth": "05",
"startDateYear": "2013",
"endDateMonth": "02",
"endDateYear": "2015"
}
]
I'm trying to add these to the params collection that I can later iterate through in an application class and add these work histories. 我试图将它们添加到params集合中,以便以后在应用程序类中进行迭代并添加这些工作历史记录。 I have the following code but it doesn't work - NoMethodError - undefined method `[]' for nil:NilClass: for this line params[:job_title][i] = jobs[i]['title'].
我有以下代码,但不起作用-NoMethodError-nil:NilClass的未定义方法'[]':为此行params [:job_title] [i] = jobs [i] ['title']。 It was just a guess, as I'm not great in ruby, so is there a way to make this work?
只是一个猜测,因为我不太懂红宝石,所以有什么办法可以使这项工作成功?
if job_count.numeric?
jobs = params['positions']['values']
for i in 0..job_count.to_i
params[:job_title][i] = jobs[i]['title']
params[:job_company_name][i] = jobs[i]['company']
params[:job_start_date][i] = jobs[i]['startDateMonth'] + '/1/' + jobs[i]['startDateYear']
params[:job_end_date][i] = jobs[i]['endDateMonth'] + '/1/' + jobs[i]['endDateYear']
params[:job_description][i] = jobs[i]['description']
end
end
EDIT: This ended up working. 编辑:这最终工作。
i = 0
jobs.each do | k, work_history |
i = i + 1
params['job_title_' + i.to_s] = work_history['title']
params['job_company_name_' + i.to_s] = work_history['company']
params['job_start_date_' + i.to_s] = work_history['startDateMonth'] + '/1/' + work_history['startDateYear']
params['job_end_date_' + i.to_s] = work_history['endDateMonth'] + '/1/' + work_history['endDateYear']
params['job_description_' + i.to_s] = work_history['description']
end
You get this error because you're trying to do ["title"]
on nil
, as said, and jobs[i]
is probably nil
because job_count
is wrong. 如您所说,由于尝试对
nil
执行["title"]
而job_count
错误,而job_count
jobs[i]
可能为nil
因为job_count
是错误的。
If you have 3 jobs, and job_count == 3
, your for
loop will do 0, 1, 2, 3. However, jobs[3]
is rightfully nil. 如果您有3个作业,而
job_count == 3
,则for
循环将执行0、1、2、3。但是, jobs[3]
正确地为nil。
Try replacing that for with for i in 0..(job_count.to_i - 1)
尝试用
for i in 0..(job_count.to_i - 1)
中的for替换为for i in 0..(job_count.to_i - 1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.