简体   繁体   English

rails数据字段json,从更多表单字段填充它

[英]rails data field json, populate it from more form fields

I have table/model of projects and for each project I want to create table/model rewards. 我有项目的表/模型,并且要为每个项目创建表/模型奖励。 I don't know if one project will have 1 or 10 rewards so I thought that best might be to store it in one json data object to avoid creating fields in table like name1, name2, nameX. 我不知道一个项目是否会有1或10个奖励,所以我认为最好将其存储在一个json数据对象中,以避免在表中创建诸如name1,name2,nameX之类的字段。 The question is how can I have more form fields and on submiting merge them and put in json data object? 问题是我如何才能拥有更多表单字段,并在提交时将它们合并并放入json数据对象中? Or is there any other way how to do that and I am missing that? 还是有其他方法可以做到这一点,而我却错过了? thanks 谢谢

On the backend you need to have two tables: projects and rewards. 在后端,您需要有两个表格:项目和奖励。 On the client side have a look at nested forms: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#M002132 在客户端,请查看嵌套表单: http : //api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#M002132

Browser will automatically take hierarchical form data, serialize and send to server. 浏览器将自动获取分层形式的数据,进行序列化并发送到服务器。 Rails will then save the data in your models. 然后,Rails将数据保存在模型中。

Here's a tutorial: http://railscasts.com/episodes/196-nested-model-form-part-1 这是一个教程: http : //railscasts.com/episodes/196-nested-model-form-part-1

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

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