【问题标题】:Rails: Array from params is not saved in DB(Postgresql), Column is of JSONB data typeRails:来自参数的数组未保存在 DB(Postgresql)中,列是 JSONB 数据类型
【发布时间】:2018-06-16 12:45:32
【问题描述】:

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

我已将它们列入白名单,并且我可以看到将散列数组传送到 Rails 服务器的参数。问题是我没有看到该列被插入到数据库中。

准确地说,字段order_placed(在这种情况下)在插入语句中被跳过。


前端代码:

<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"=&gt;[{"itemname"=&gt;"Laptop", "quantity"=&gt;"1", "unitprice"=&gt;"10", "tax"=&gt;"0", "discount"=&gt;"0", "itemtotalprice"=&gt;"10"}, {"itemname"=&gt;"Cable", "quantity"=&gt;"0", "unitprice"=&gt;"0", "tax"=&gt;"0", "discount"=&gt;"0", "itemtotalprice"=&gt;"0"}] 中的哈希数组在插入语句中被跳过。预计将保存在 DB 中 JSONB 数据类型的 order_placed 字段中。

【问题讨论】:

  • 你已经允许 order_placedorderplaced 它是不同的属性。
  • 谢谢Зелёный。这有助于我结合@Daniel 的评论解决我的问题。
  • 这不是问题,是错字。

标签: ruby-on-rails arrays ruby postgresql


【解决方案1】:
<td><input name="orderplaced[][itemname]" type="text" /></td>

在名称属性中输入顺序。

【讨论】:

  • 感谢您的回复。这有助于我结合上述评论解决问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 2019-05-20
  • 2015-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多