如何在保存主要问题后进行多次插入?
表:
flow_budgets
|id| |proyected_money|
1 5000
category_expense_budgets
|id| |amount| |flow_budget_id| |category_expense_id|
1 1000 1 1
2 2000 1 1
3 3000 1 1
4 4000 1 2
5 5000 1 2
6 6000 1 2
category_expenses
|id| |name| |analysis_expense_id|
1 Category 1 1
2 Category 2 1
3 Category 3 2
4 Category 4 2
analysis_expenses
|id| |name|
1 Analysis 1
2 Analysis 2
这是控制器:
def new_flow
@analysis_expenses = AnalysisExpense.all
@obj_flow = FlowBudget.new(params[:obj_flow])
end
def create_flow
obj_flow = FlowBudget.new(params[:obj_flow])
obj_flow.save()
if obj_flow.save()
@flow_budget_id = FlowBudget.last.id
obj_proyected = CategoryExpenseBudget.new
end
end
以下是观点:
<% form_for :obj_flow, :url => {:controller=>"flow_budget",:action=>'create_flow'} do |f|%>
<%= f.text_field :proyected_money %>
<% @analysis_expenses.each do |analysis_expense| %>
<label><%= analysis_expense.name %></label>
<%= text_field_tag "proyected_analysis_expenses",{},:name => "proyected_analysis_expense[amount][]", :id => "proyected_analysis_expense_#{analysis_expense.id}" %>
<table>
<% analysis_expense.category_expenses.each do |category_expense|%>
<tr>
<td><%= category_expense.name %>:</td>
<td><%= text_field_tag "proyected_category_expenses",{},:name => "proyected_category_expense[name][]", :id => "proyected_category_expense_#{category_expense.id}" %></td>
</tr>
<% end %>
</table>
<% end %>
<% end %>
这是我的日志:
Processing FlowBudgetController#create_flow (for 127.0.0.1) [POST]
Parameters: {"proyected_money"=>"8000",
"proyected_category_expense"=>{"amount"=>["2100", "2500" ],
"proyected_analysis_expense"=>{"amount"=>["1000", "1100", "1200", "1300" ]}
INSERT INTO `flow_budgets` (`proyected_money` ) VALUES(8000)
我要保存
INSERT INTO `flow_budgets` (`proyected_money` ) VALUES(8000)
INSERT INTO `category_expense_budgets` (`amount`,'flow_budget_id','category_expense_id' ) VALUES(1000,1,1)
INSERT INTO `category_expense_budgets` (`amount`,'flow_budget_id','category_expense_id' ) VALUES(1100,1,2)
INSERT INTO `category_expense_budgets` (`amount`,'flow_budget_id','category_expense_id' ) VALUES(1200,1,3)
INSERT INTO `category_expense_budgets` (`amount`,'flow_budget_id','category_expense_id' ) VALUES(1300,1,4)
请有人帮助我吗?
答案 0 :(得分:1)
我认为你要找的是accepts_nested_attributes_for :category_expense_budgets
我认为你的模型ObjFlow有has_many :category_expense_budgets
。
然后它应该工作,如果您添加嵌套属性的接受并格式化您的表单,以便params哈希得到正确的格式。我认为最简单的方法是:
<% form_for @obj_flow, :url => {:controller=>"flow_budget",:action=>'create_flow'} do |f|%>
<%= f.text_field :proyected_money %>
<% @analysis_expenses.each do |analysis_expense| %>
<% f.fields_for analysis_expense do |nested_f| %> # THIS IS A NEW IMPORTANT LINE
<label><%= analysis_expense.name %></label>
<%= nested_f.text_field "proyected_analysis_expenses",{},:name => "proyected_analysis_expense[amount][#{analysis_expense.id}]", :id => "proyected_analysis_expense_#{analysis_expense.id}" %>
<table>
<% analysis_expense.category_expenses.each do |category_expense|%>
<tr>
<td><%= category_expense.name %>:</td>
<td><%= text_field_tag "proyected_category_expenses",{},:name => "proyected_category_expense[name][#{analysis_expense.id}]", :id => "proyected_category_expense_#{category_expense.id}" %></td>
</tr>
<% end %>
</table>
<% end %>
<% end %>
<% end %>
如果您愿意,请在此处阅读:http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html