【发布时间】: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 %>
<%= f.label :totalprice %>
<%= f.text_field :totalprice %>
<%= 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"}] 中的哈希数组在插入语句中被跳过。预计将保存在 DB 中 JSONB 数据类型的 order_placed 字段中。
【问题讨论】:
-
你已经允许
order_placed和orderplaced它是不同的属性。 -
谢谢Зелёный。这有助于我结合@Daniel 的评论解决我的问题。
-
这不是问题,是错字。
标签: ruby-on-rails arrays ruby postgresql