简体   繁体   English

如何在不使用Rails的循环的情况下将文本字段数组数据存储在数据库中

[英]How to Store the text fields array data in database without using loop using rails

I am new in ror and I am trying from 4 hours to store the data of form of textfields array like this 我是ror的新手,我试图从4个小时开始存储像这样的textfields数组形式的数据

<input type="text" name="custom_field[names][]" class="form-control full-width" placeholder =  "Name">

<input type="text" name="custom_field[length_limit][]" class="form-control full-width" placeholder =  "Length Limit">

I want to store the arrays coming into the form in database columns of name and length limit. 我想将进入表单的数组存储在名称和长度限制的数据库列中。 I don't want to use loop to do this job. 我不想使用循环来完成这项工作。

I am doing this in controller 我正在控制器中执行此操作

user = CustomField.create(:name=> params[:names])

But it is giving ERROR: null value in column "name" violates not-null constraint DETAI 但是它给出了ERROR: null value in column "name" violates not-null constraint DETAI

params are 参数是

{"names"=>["tester", "another tester"], "length_limit"=>["aaaaaa222", "aaaaaa222"]

I am using postgresql 我正在使用postgresql

How can I do this? 我怎样才能做到这一点?

params[:names] is an array, so it doesn't make sense to save it to a string/text field in postgres, which is what CustomField.create(:name=> params[:names]) does. params[:names]是一个数组,因此将其保存到postgres中的字符串/文本字段中是没有意义的, CustomField.create(:name=> params[:names])这样做的。

Essentially, it expands to CustomField.create(:name=> ["tester", "another tester"]) . 从本质上讲,它扩展为CustomField.create(:name=> ["tester", "another tester"]) If you want to create a separate CustomField record for each name, then you could do something like: 如果要为每个名称创建单独的CustomField记录,则可以执行以下操作:

params[:names].each do |name|
  CustomField.create(:name => name)
end


To create the CustomField records with the corresponding length_limit , you could do something like: 要使用相应的length_limit创建CustomField记录,可以执行以下操作:

params[:names].zip(params[:length_limit]).each do |name, limit|
  CustomField.create(:name => name, :length_limit => limit)
end

Note that this doesn't account for errors where the name s and length_limit s don't match. 请注意,这不能解决name s和length_limit不匹配的错误。 You might want to add some validation to make sure that all the required data is present. 您可能需要添加一些验证,以确保所有必需的数据都存在。

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

相关问题 如何使用Rails在数据库中存储数字数组 - How to store an array of numbers in database using rails 如何使用Rails在数据库中存储下拉列表和其他选项文本输入数据? - How to store the dropdown, other option text input data in database using ruby on rails? 如何使用Ruby on Rails将字符串存储为数据库列中的数组 - How to store string as array in database column using Ruby on Rails Rails数据库-如何使用用户密码存储加密数据? - Rails database - how to store encrypted data using the user's password? Rails 表单:如何将两个 text_fields 存储在一个数组中 - Rails forms: how to store two text_fields in one array 如何在不使用数据库的情况下将 Ruby object 存储在我的 Rails 应用程序中? - How do I store a Ruby object in my Rails app without using a database? 将数据存储在没有数据库的Ruby on Rails中 - Store data in Ruby on Rails without Database 遍历数据,以数组形式存储在Ruby on Rails中 - Loop through data, store as array in Ruby on Rails 如何在Rails 3.0上使用ruby将组合框与数据库字段连接 - How to connect the combo box with database fields using ruby on rails 3.0 如何使用rails将多个字段保存在一个数据库列中 - How to save multiple fields in one database column using rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM