简体   繁体   English

RoR编辑嵌套表中的联接表的值

[英]RoR edit value of join table from nested form

I am a noob on Ror. 我是Ror的菜鸟。 Been looking for my problem answers for 3 days now, I have been looking for the answers but can't find one with my specific problem. 一直在寻找我的问题答案已有3天了,我一直在寻找答案,但找不到与我的特定问题有关的答案。 ( I even found it hard to write the right title) (我什至发现很难写出正确的标题)

So I have been trying to build a nested form in RoR. 因此,我一直在尝试在RoR中构建嵌套表单。 I have a simple order form that enable users to specify the quantity they ordered in that form. 我有一个简单的订购单,使用户可以指定他们在该表单中订购的数量。 The form will only store the value into the database if the quantity text field is not empty. 如果数量文本字段不为空,则该表单仅将值存储到数据库中。

The Order form is simply look like this: 订单表单看起来像这样:

形式

I am storing the quantity data into the join table between order and inventory which has many to many relationship through Inventory_orders table. 我将数量数据存储到订单和库存之间的联接表中,该表通过Inventory_orders表具有多对多关系。 now in the Inventory_orders table instead of only having orders_id and inventories_id , I also add the quantity column. 现在,在Inventory_orders表中,而不是仅具有orders_id和inventories_id,我还添加了数量列。

now I have been able to get the form working with the code below: Controller: 现在,我已经可以使用下面的代码来处理表单:控制器:

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

View: 视图:

<%= 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 %>

Model: 模型:

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

Now when creating new Orders form , the application works and store the data that I want in the inventory_orders table. 现在,在创建新的Orders表单时,该应用程序将工作并将所需的数据存储在库存表中。

The problem occurs when I try to edit the form. 当我尝试编辑表单时,会出现问题。 When trying to click on edit button I get this output in my View file: 当尝试单击编辑按钮时,我的视图文件中得到以下输出:

for example this is what I Input into the form: 例如,这就是我输入的形式: 在此处输入图片说明

when I try to edit the form this is what I get: 当我尝试编辑表单时,这就是我得到的:

在此处输入图片说明

this is my controller for edit: 这是我的编辑控制器:

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

 end

I have been looking at the psql database schema by manual do sql query as follow: 我一直在通过手动执行sql查询来查看psql数据库架构,如下所示:

select * from inventory_orders where inventory_orders.order_id = 63; 从库存订单中选择*,其中库存订单.order_id = 63;

and get this as result: 并得到以下结果: 在此处输入图片说明

now it seems that the fields_for Inventory_orders get the number of rows returned , but I don't get why all the quantity also get displayed 4 times for each product. 现在看来,fields_for Inventory_orders可以获取返回的行数,但是我不明白为什么每种产品的所有数量也会显示4次。 Also how can I ensure that when I try to edit quantity for product “aaa” it will only display one number_field with what users has input before. 另外,当我尝试编辑产品“ aaa”的数量时,如何确保它仅显示一个number_field以及用户之前输入的内容。

Sorry for the long post,otherwise I am not sure how to clearly convey my meaning. 对不起,很长的帖子,否则我不确定如何清楚地表达我的意思。

EDITED this to show my Inventory Model: 编辑此内容以显示我的库存模型:

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

You need to use the following: 您需要使用以下内容:

//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 %>

The issue is that since f.fields_for populates a form based on the built associated objects, if you're passing 4 fully constructed objects through the edit action, fields_for is going to populate all of them each time. 问题在于,由于f.fields_for将基于已构建的关联对象填充表单,因此如果您要通过edit动作传递4个完全构建的对象,则fields_for都将填充所有这些对象。

What you need is to use the instance of the associated data . 您需要使用关联数据实例


I think your code could be improved a lot: 我认为您的代码可以改进很多:

#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 %>

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

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