Rails:参数中的数组不保存在DB(Postgresql)中,列是JSONB数据类型



我正在尝试将参数数组从GUI发送到Rails控制器。

我已经将它们列入白名单,我可以看到参数将哈希数组携带到 Rails 服务器。问题是我没有看到该列入到数据库中。

确切地说,字段order_placed(在本例中(在 Insert 语句中被跳过。


前端代码:

<table id="tabledata">
    <thead>
        <th>Item Name</th>
        <th>Quantity</th>
        <th>Unit Price</th>
        <th>Tax</th>
        <th>Discount</th>
        <th>Item Total Price</th>
    </thead>
    <tbody id="input"></tbody>
    <tbody id="template">
          <%= form_for @order do |f| %>
          <%= f.label :ordertype %>
          <%= f.text_field :ordertype %> &nbsp; &nbsp; &nbsp; &nbsp;
          <%= f.label :totalprice %>
          <%= f.text_field :totalprice %> &nbsp;&nbsp;&nbsp;&nbsp; 
          <%= f.label :paymentmethod %>
          <%= f.text_field :paymentmethod %>
          <br>
        <tr>
            <td><input name="orderplaced[][itemname]" type="text" /></td>
            <td><input name="orderplaced[][quantity]" type="text" /></td>
            <td><input name="orderplaced[][unitprice]" type="text" /></td>
            <td><input name="orderplaced[][tax]" type="text" /></td>
            <td><input name="orderplaced[][discount]" type="text" /></td>
            <td><input name="orderplaced[][itemtotalprice]" type="text" /></td>
        </tr>
        <tr>
            <td><input name="orderplaced[][itemname]" type="text" /></td>
            <td><input name="orderplaced[][quantity]" type="text" /></td>
            <td><input name="orderplaced[][unitprice]" type="text" /></td>
            <td><input name="orderplaced[][tax]" type="text" /></td>
            <td><input name="orderplaced[][discount]" type="text" /></td>
            <td><input name="orderplaced[][itemtotalprice]" type="text" /></td>
        </tr>
 </tbody>
</table>
<label id="ActionAddRow">Add Row</label>
<%= f.submit %>
 <% end %>

控制器代码:

class OrdersController < ApplicationController
    def new
        @order=Order.new
    end
    def create
        @order=Order.new(order_params)
        @order.customer=Customer.first
        @order.save
    end
    private
        def order_params
            params.require(:order).permit(:ordertype, :totalprice, :paymentmethod, order_placed: [:itemname, :quantity, :unitprice, :tax, :discount, :itemtotalprice])
        end
end

服务器端问题:

Started POST "/orders" for 127.0.0.1 at 2018-01-07 12:26:09 +0530
Processing by OrdersController#create as HTML
  Parameters: {"order"=>{"ordertype"=>"Home delivery", "totalprice"=>"10", "paymentmethod"=>"Cash"}, "utf8"=>"Γ£ô", "authenticity_token"=>"BFl2CfwytjM48bHIIUsrjNqk8bU75CHx/V3TH0OlviGabNmxDd3HXuhK0xHKHJwvbNvgD8Hivf62PYwFPDIIag==", "orderplaced"=>[{"itemname"=>"Laptop", "quantity"=>"1", "unitprice"=>"10", "tax"=>"0", "discount"=>"0", "itemtotalprice"=>"10"}, {"itemname"=>"Cable", "quantity"=>"0", "unitprice"=>"0", "tax"=>"0", "discount"=>"0", "itemtotalprice"=>"0"}], "commit"=>"Create Order"}
  Customer Load (3.0ms)  SELECT  "customers".* FROM "customers" ORDER BY "customers"."id" ASC LIMIT $1  [["LIMIT", 1]]
   (0.0ms)  BEGIN
  SQL (1.0ms)  INSERT INTO "orders" ("ordertype", "totalprice", "paymentmethod", "created_at", "updated_at", "customer_id") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["ordertype", "Home delivery"], ["totalprice", 10.0], ["paymentmethod", "Cash"], ["created_at", "2018-01-07 06:56:10.053668"], ["updated_at", "2018-01-07 06:56:10.053668"], ["customer_id", 1]]
   (1.3ms)  COMMIT

字段order_placed,其中包含参数中的哈希数组"orderplaced"=>[{"itemname"=>"Laptop", "quantity"=>"1", "unitprice"=>"10", "tax"=>"0", "discount"=>"0", "itemtotalprice"=>"10"}, {"itemname"=>"Cable", "quantity"=>"0", "unitprice"=>"0", "tax"=>"0", "discount"=>"0", "itemtotalprice"=>"0"}]在 Insert 语句中被跳过。这应该保存在数据库中order_placed JSONB数据类型的字段中。

<td><input name="orderplaced[][itemname]" type="text" /></td>

在名称属性中输入顺序。

相关内容

  • 没有找到相关文章

最新更新