2014-11-09 75 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