RoR从嵌套表单

时间:2015-10-14 03:53:16

标签: ruby-on-rails nested-forms has-many-through form-for

我是Ror的菜鸟。一直在寻找我的问题答案3天,我一直在寻找答案,但找不到我的具体问题。 (我甚至发现很难写出正确的标题)

所以我一直在尝试在RoR中构建一个嵌套表单。我有一个简单的订单表单,使用户能够指定他们在该表单中订购的数量。如果数量文本字段不为空,表单将仅将值存储到数据库中。

订单表格看起来像这样:

oder form

我将数量数据存储在订单和库存之间的连接表中,通过Inventory_orders表存在多对多的关系。现在在Inventory_orders表中,而不是只有orders_id和inventoryories_id,我还添加了数量列。

现在我已经能够使用以下代码获取表单: 控制器:

def new
    @order = Order.new
    @customer = Customer.all
    @inventories_list = Inventory.all 
    @inventory_order = @order.inventory_orders.build
  end

  def create

    @order = Order.new(order_params)
    @inventories_list = Inventory.all #controller can call any model

    respond_to do |format|
      if @order.save
        format.html { redirect_to @order, notice: 'Order was successfully created.' }
        format.json { render :show, status: :created, location: @order }
      else
        format.html { render :new }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

 def order_params
      params.require(:order).permit(:customer_id, :order_ids => [],:inventory_orders_attributes => [:id, :quantity, :inventory_id ])
    end

查看:

<%= form_for(@order) do |f| %>
<div id = “Main_Container">
*** Some Code  ***
                <table id = "inventory_table">
                  <tr>
                    <td class = "prodCodeTitle"><h3>Prod Code</h3></td>
                    <td class = "prodResult"><h3>Quantity</h3></td>
                    <td class = "prodResult"><h3>Size</h3></td>
                    <td class = "prodResult"><h3>Price</h3></td>
                  </tr>

//Here display all the inventories list 
 <% @inventories_list.each do |t| %>
                  <tr>

                    <td class ="prodResult"><%= link_to t.pName, inventory_path(t), :remote => true %></td>
                    <td class = “prodResult">
        //nested form for the join table
                      <%= f.fields_for :inventory_orders do |qty| %>
                         <%= qty.hidden_field :inventory_id , value: t.id %>
                         <%= qty.number_field :quantity %>
                     <%end%>
                    </td>
                    <td class = "prodResult"><%= t.pMeter %></td>
                    <td class = "prodResult"><%= t.pSellPrice %></td>

                  </tr>
                    <% end %>
             *** Some Code***
<% end %>

型号:

class Order < ActiveRecord::Base
    belongs_to :customer
    has_many :inventory_orders
    has_many :inventories, through: :inventory_orders
    validates :customer_id, :presence => true

    accepts_nested_attributes_for :inventory_orders, :reject_if => lambda { |a| a[:quantity].blank?}

end

class InventoryOrder < ActiveRecord::Base
    belongs_to :inventory
    belongs_to :order


    validates :quantity, :presence => true
end

现在,在创建新的订单表单时,应用程序会在inventory_orders表中运行并存储我想要的数据。

当我尝试编辑表单时出现问题。当我尝试点击编辑按钮时,我在我的View文件中得到了这个输出:

例如,这是我在表单中输入的内容: enter image description here

当我尝试编辑表单时,这就是我得到的:

enter image description here

这是我的编辑控制器:

def edit
    @order = Order.find(params[:id])
    @customer = Customer.all
    @inventories_list = Inventory.all

 end

我一直在通过手动执行sql查询来查看psql数据库模式,如下所示:

从inventory_orders中选择*,其中inventory_orders.order_id = 63;

得到这个结果: enter image description here

现在看来fields_for Inventory_orders获得了返回的行数,但我不明白为什么所有数量也会为每个产品显示4次。另外,我如何确保当我尝试编辑产品“aaa”的数量时,它将只显示一个用户之前输入的数字字段。

对于长篇文章感到抱歉,否则我不确定如何清楚地传达我的意思。

EDITED 这将显示我的库存模型:

Class Inventory < ActiveRecord::Base
  has_many :inventory_orders
  has_many :orders, through: :inventory_orders
end

1 个答案:

答案 0 :(得分:0)

您需要使用以下内容:

//Here display all the inventories list 
 <% @inventories_list.each do |t| %>
    <%= link_to t.pName, inventory_path(t), :remote => true %>
    <%= f.fields_for :inventory_orders, t do |qty| %>
            <%= qty.hidden_field :inventory_id , value: t.id %>
            <%= qty.number_field :quantity %>
    <% end %>
    <%= t.pMeter %>
    <%= t.pSellPrice %
<% end %>

问题是,由于f.fields_for根据构建的关联对象填充表单,如果您通过edit操作传递了4个完全构造的对象, fields_for每次都会填充所有

您需要的是使用instance of the associated data

我认为您的代码可以得到很多改进:

#app/controllers/orders_controller.rb
class OrdersController < ApplicationController
    def new
        @order = Order.new
        @inventory = Inventory.all 
        @inventory_order = @order.inventory_orders.build
    end

    def edit
        @order = Order.find params[:id]
        @inventory = Inventory.all
    end
end

#app/views/orders/new.html.erb
<%= form_for @order do |f| %>
    <% @inventory.each do |inventory| %>
       <%= f.fields_for :inventory_orders, item do |item| %>
          <%= item.text_field :quantity %>
       <% end %>
    <% end %>
    <%= f.submit %>
<% end %>

#app/views/orders/edit.html.erb
<%= form_for @order do |f| %>
   <%= f.fields_for @order.inventory_orders do |item| %>
       <%= item.text_field :quantity %>
   <% end %>
   <%= f.submit %>
<% end %>