2014-11-09 78 views
0

所以我有这样的要求如下:在Ruby中的每个循环on Rails的

{"utf8"=>"✓", 
"student_work"=>{"work_experiences_attributes"=>{"1415498778636"=>{"company_name"=>"Company1", 
"start_year"=>"2014", 
"end_year"=>"2014", 
"job_title"=>"Title1", 
"job_description"=>"test1"}, 
"1415498795509"=>{"company_name"=>"Company2", 
"start_year"=>"2014", 
"end_year"=>"2014", 
"job_title"=>"Title2", 
"job_description"=>"Test2"}}}, 
"commit"=>"Next"} 

或更容易:

student_work[work_experiences_attributes][1415498778636][company_name]:Company1 
student_work[work_experiences_attributes][1415498778636][start_year]:1 
student_work[work_experiences_attributes][1415498778636][start_year]:2014 
student_work[work_experiences_attributes][1415498778636][end_year]:2 
student_work[work_experiences_attributes][1415498778636][end_year]:2014 
student_work[work_experiences_attributes][1415498778636][job_title]:Title1 
student_work[work_experiences_attributes][1415498778636][job_description]:test1 
student_work[work_experiences_attributes][1415498795509][company_name]:Company2 
student_work[work_experiences_attributes][1415498795509][start_year]:3 
student_work[work_experiences_attributes][1415498795509][start_year]:2014 
student_work[work_experiences_attributes][1415498795509][end_year]:4 
student_work[work_experiences_attributes][1415498795509][end_year]:2014 
student_work[work_experiences_attributes][1415498795509][job_title]:Title2 
student_work[work_experiences_attributes][1415498795509][job_description]:Test2 

如何访问使用Ruby中的每个循环的每个值?我还是Ruby的新手,请帮忙。由于

更新 那些号码(1415498778636和1415498795509)总是在变化的,所以回路必须能够处理任何不同类型的数字

+0

今后,请简化您的例子尽可能的。在这里,例如,有很多键值对可以被删除,而不会使示例没有那么有意义。此外,缩短名称使其更具可读性。 – 2014-11-09 03:17:27

回答

1
params['student_work']['work_experiences_attributes'].values.each do |hash| 
    hash.each do |key, value| 
    # ... 
    end 
end 
0

你的意思,而不是响应请求权? 你可以这样做:

attr=params[:student_work].keys.each do |key| 
    key.to_i > 0 
end[0] 
params[:student_work][attr].each do |key, value| 
# do something 
end 
+0

好的。宽度是固定的? – emaxi 2014-11-09 03:05:22