I have one form in view for the @item_list
:
我有一个表格来查看@item_list:
<%= form_for @item_list do |f|%>
some meta-info to describe item_list
<%end%>
and also I have a render partial called _item.html.erb
:
我还有一个名为_item.html.erb的渲染部分:
<%= form_for @item_list.item do |f|%>
some meta-info to describe item_list.item
<%end%>
In my index view, I have a button called add item
, which will call a jquery ajax function to append this partial to a div block, and another button called done
, which I expect to store all the info in all the forms at one time.
在我的索引视图中,我有一个名为add item的按钮,它将调用jquery ajax函数将此部分附加到div块,另一个名为done的按钮,我希望一次性存储所有表单中的所有信息。
When I try to save these, the parameter doesn't include the item information. I guess that is because I didn't update the item_list
at the backend when I tried to add a new item. I hope you can provide me some ideas on this.
当我尝试保存这些时,参数不包括项目信息。我想这是因为当我尝试添加新项目时,我没有更新后端的item_list。我希望你能就此提出一些想法。
1 个解决方案
#1
3
You're going to want to setup a nested form for this. There are many other questions on * about nested forms, but here's the gist:
您将要为此设置嵌套表单。关于嵌套表单的*还有很多其他问题,但这里有一个要点:
- Create a new empty object in your controller to populate an item
@item_list.items.build
assuming an ItemList has_many Items - 假设ItemList has_many项目,在控制器中创建一个新的空对象以填充项目@ item_list.items.build
- Add
accepts_nested_attributes_for :item
in your Item model - 在Item模型中添加accepts_nested_attributes_for:item
- Add
attr_accessible :items_attributes
in your Item model as well - 在Item模型中添加attr_accessible:items_attributes
-
You're going to need to pass your form builder to the partial, so something like this:
你需要将表单构建器传递给partial,所以像这样:
_form.html.erb <%= form_for @item_list do |f| %> <% @item_list.items.each do |item| %> <%= render 'item', :f => f %> <% end %> <% end %> _item.html.erb <%= f.fields_for :items, item do |item_form| %> <%= item_form.text_field ... %> <% end %>
If you need more specifics, please provide us with some more code, including your models with relationships. I'll leave the javascript to you at this point.
如果您需要更多细节,请向我们提供更多代码,包括您的关系型号。我现在将把javascript留给你。
#1
3
You're going to want to setup a nested form for this. There are many other questions on * about nested forms, but here's the gist:
您将要为此设置嵌套表单。关于嵌套表单的*还有很多其他问题,但这里有一个要点:
- Create a new empty object in your controller to populate an item
@item_list.items.build
assuming an ItemList has_many Items - 假设ItemList has_many项目,在控制器中创建一个新的空对象以填充项目@ item_list.items.build
- Add
accepts_nested_attributes_for :item
in your Item model - 在Item模型中添加accepts_nested_attributes_for:item
- Add
attr_accessible :items_attributes
in your Item model as well - 在Item模型中添加attr_accessible:items_attributes
-
You're going to need to pass your form builder to the partial, so something like this:
你需要将表单构建器传递给partial,所以像这样:
_form.html.erb <%= form_for @item_list do |f| %> <% @item_list.items.each do |item| %> <%= render 'item', :f => f %> <% end %> <% end %> _item.html.erb <%= f.fields_for :items, item do |item_form| %> <%= item_form.text_field ... %> <% end %>
If you need more specifics, please provide us with some more code, including your models with relationships. I'll leave the javascript to you at this point.
如果您需要更多细节,请向我们提供更多代码,包括您的关系型号。我现在将把javascript留给你。