从逗号分隔的 ID 一次创建多个记录 [英] Creating multiple records at once from comma seperated IDs
本文介绍了从逗号分隔的 ID 一次创建多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有一个文本字段,用户可以在其中放置逗号分隔的卡 ID,例如 1,2,3.当这个用户提交表单时,我想为他提交的每个 ID 创建一个新记录.所以一比一,一比二,一比三.
so I have a textfield, where a user can put comma seperated card IDs, for example 1,2,3. When this user submits the form, I want to create a new record of each ID he submits. So one for 1, one for 2, one for 3.
我该怎么做?
谢谢
推荐答案
您必须从控制器中的文本字段解析输入值.例如:
You must parse input value from textfield in your controller. For example:
def update
ids_input=params[:ids]
ids_input.split(',').each { |id|
Card.create(:some_id=>id,:other_column=>'some_value',:related_user=>current_user)
}
end
这篇关于从逗号分隔的 ID 一次创建多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文