简体   繁体   English

Rails-将嵌套参数保存为表格

[英]Rails - Save Nested Params for Form

My params for a nested form look like this: 我的嵌套表单参数如下所示:

"answer_group"=>{"539"=>{"answer_text"=>{"Scoreboard Report"=>"0", "GBG Observation"=>"1", "Data Review"=>"1", "Coach Probe"=>"0", "Team Assignment"=>"0", "Probe"=>"0", "Joint Probe"=>"0", "Weekly Scoreboard"=>"0"}}, "556"=>{"1034"=>{"column1"=>"n/o", "column2"=>"1", "column3"=>"is", "column4"=>"this", "column5"=>"working"}, "1035"=>{"column1"=>"n/o", "column2"=>"1", "column3"=>"", "column4"=>"", "column5"=>""}}

How do I save and access the nested params if keys are question.id ? 如果键是question.id如何保存和访问嵌套参数? I have attributes for column1 ... column5 in the table, everything else should save to answer_text . 我在表中具有column1 ... column5属性,其他所有内容都应保存到answer_text

Something like answer_attributes["#{question.id}"] ? 类似于answer_attributes["#{question.id}"]吗?

For saving, take a closer look at accepts_nested_attributes_for . 为了保存,请仔细查看accepts_nested_attributes_for The Rails framework provides a mechanism for doing what you've described. Rails框架提供了一种执行您所描述的功能的机制。

For accessing, what you have would work but the interpolated string is the same thing as answer_attributes[question.id.to_s] , which is arguably more readable. 对于访问,您可以使用,但是插入的字符串与answer_attributes[question.id.to_s]是同一件事,可以说更具可读性。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM