使用单个表单创建多个记录(不是嵌套属性) [英] Create multiple records using single form (Not nested attributes)
本文介绍了使用单个表单创建多个记录(不是嵌套属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用程序中,我有一个具有内容和作者属性的思考模型。
我想用新形式一次创建多个想法。但这不是嵌套表格的情况,因为我没有使用任何关联的模型。
请提出一些解决方案。
在此先感谢!
解决方案
您可以尝试以下解决方案
在您的查看文件中
<%= form_tag your_action_path do%>
<%4times do | i |%>
内容:<%= text_area_tag:thought_content,,::name => 思想[] [内容]%>
作者:<%= text_field_tag:thought_author,,::name => 思想[] [作者]%>
<%end%>
<%= submit_tag提交%>
<%end%>
控制器代码:
def your_action
params [:思考] .each do | thought_params |
Thought.create(thought_params)
结束
###
#所有进一步代码#
###
结束
希望它对您有用:)
In my application I have a Thought model which has content and author attributes.
I want to create multiple thoughts at once using new form. But this is not a case of nested forms as i am not using any associated models.
Please suggest some solution. Thanks in advance!
解决方案
You can try with the below solution
In your View File
<%= form_tag your_action_path do %>
<% 4.times do |i|%>
Content : <%= text_area_tag :thought_content, "", :name => "thoughts[][content]" %>
Author : <%= text_field_tag :thought_author, "", :name => "thoughts[][author]" %>
<% end %>
<%= submit_tag "Submit" %>
<% end %>
Controller Code:
def your_action
params[:thoughts].each do |thought_params|
Thought.create(thought_params)
end
###
#Any further code#
###
end
Hope it works for you :)
这篇关于使用单个表单创建多个记录(不是嵌套属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文