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

使用的是一个一对多关联,代码如下:view: partial
名称:<input type="text" name="invoice[][name]" />
描述:<input type="text" name="invoice[][description]" /><br/>
rhtml:
<% form_tag "/purchase/save_order" do -%>
<p><label for="order_name">订单:</label><%= text_field :order, :name %></p>
<p>物 品:    <%= link_to_remote "增加物品",
                     :update => 'mat',
                     :url => {:action => :add_field },
                     :position => 'bottom' %></p>
<div id="mat">
    <%= render :partial => 'mat_input' %>
</div>
<p><%= submit_tag "提 交", :class => "submit" %></p>
controller:
def save_order
    begin
      @order = Order.new(params[:order])
      total = params[:invoice].length
      params[:invoice].each do |invoice|
        @invoice = Invoice.new(invoice)
        @order.invoices << @invoice
      end
      if request.post? and @order.save
        flash[:notice] = "#{total}条物品记录已保存"
      end
    rescue
      raise
    end
    redirect_to(:action => "index")
end

def add_field
    render :partial => 'mat_input'
end

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

你可能感兴趣的:(Flash,Ruby,Rails)