简体   繁体   English

通过动态参数字段

[英]passing dynamic params field

Im working with Rails 3.0.3 我正在使用Rails 3.0.3

I want to create bills in my App. 我想在我的应用程序中创建账单。 Each Bill has many entries (Material, how much of that and the Price) 每个法案都有很多条目(材料,数量和价格)

The Problem i have, is that i want to write the bill and the entries and then save both at the same time. 我的问题是我想写账单和分录,然后同时保存两者。 So when you click on save Bill, the Bill + each Entry should be created (saved in the db). 因此,当您单击保存帐单时,应创建帐单+每个条目(保存在数据库中)。

I can write the bill + each entry (with javascript), but i dont know how i could save both of them. 我可以写账单+每个条目(使用javascript),但我不知道如何保存它们。 Right now i can only save the bill it selft. 现在,我只能保存自己的账单。 Is it possible to pass a dynamic field via params so i can handle that in the bills controller to save? 是否可以通过参数传递动态字段,以便我可以在票据控制器中进行处理以进行保存? How would you implement this? 您将如何实施?

What you are looking for is called nested form, you have a main form for your bill and multiple forms that are dynamically generated as children of this general form using fields_for like this: 您要查找的内容称为嵌套表单,您有一个主表单用于帐单,还有多种表单,这些表单是使用fields_for作为该常规表单的子级动态生成的,如下所示:

<% form_for @bill do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <% f.fields_for :entry do |builder| %>
    <%= render "entry", :f => builder %>
  <% end %>
  <p><%= f.submit "Submit" %></p>
<% end %>

Of course you will need some js for the dynamic creation of the different entries, here you have a couple of railscasts that will be helpfull. 当然,您将需要一些js来动态创建不同的条目,在这里,您会遇到一些有用的railscasts。

Nested model form Part 1 Nested model form Part 2 嵌套模型表单第1部分 嵌套模型表单第2部分

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM