所以我有以下要求:
{"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 :(得分:1)
params['student_work']['work_experiences_attributes'].values.each do |hash|
hash.each do |key, value|
# ...
end
end
答案 1 :(得分:0)
实际上student_work[work_experiences_attributes]
是哈希,所以你可以使用each
方法。
答案 2 :(得分: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