一个表单提交多条记录的处理(Ruby on Rails)

来源:互联网 发布:淘宝网老年女夏装 编辑:程序博客网 时间:2024/04/29 14:53

使用的是一个一对多关联,代码如下:view: partial
名称:<input type="text" name="invoice[][name]" />
描述:<input type="text" name="invoice[][description]" /><br/>
rhtml:

  1. <% form_tag "/purchase/save_order" do -%>   
  2. <p><label for="order_name">订单:</label><%= text_field :order:name %></p>   
  3. <p>物 品:    <%= link_to_remote "增加物品",   
  4.                      :update => 'mat',   
  5.                      :url => {:action => :add_field },   
  6.                      :position => 'bottom' %></p>   
  7. <div id="mat">   
  8.     <%= render :partial => 'mat_input' %>   
  9. </div>   
  10. <p><%= submit_tag "提 交":class => "submit" %></p>   
  11. controller:   
  12. def save_order   
  13.     begin  
  14.       @order = Order.new(params[:order])   
  15.       total = params[:invoice].length   
  16.       params[:invoice].each do |invoice|   
  17.         @invoice = Invoice.new(invoice)   
  18.         @order.invoices << @invoice  
  19.       end  
  20.       if request.post? and @order.save   
  21.         flash[:notice] = "#{total}条物品记录已保存"  
  22.       end  
  23.     rescue  
  24.       raise  
  25.     end  
  26.     redirect_to(:action => "index")   
  27. end  
  28.   
  29. def add_field   
  30.     render :partial => 'mat_input'  
  31. end  

关键在于partial中文本框name属性的设置

原创粉丝点击