对Strong Params(我认为)有一点问题,虽然这可能是一个“第8层”问题: - )
我有一个将数据传递给create方法的表单。数据显示在服务器控制台中,但数据不会保存到表中。查看服务器输出,我看到有以下错误/声明:未允许的参数:utf8,authenticity_token,wo,commit
我的create方法如下所示:
def create
@wo = WorkOrder.new(workorder_params)
puts "outputting varibles"
puts "\n\n"
puts @wo[:wo]
if @wo.save
redirect_to(:action => 'index')
else
render('new')
end
end
private
def workorder_params
params.permit(
:wo,
:work_order_ref,
:customer_id,
:customer_contact_name,
:customer_contact_number,
:customer_contact_email,
:delivery_terms_for_order,
:customer_po_number,
:sage_call_off_number,
:order_detail_1,
:order_detail_2,
:dt_customer_ordered,
:dt_customer_required,
:dt_orig_promise,
:dt_current_fulfill,
:sales_order_number,
:sales_person,
:customer_address_id,
:shipping_id
)
end
我的表单看起来像这样
<div class="new work order">
<h2>Create New Work Order</h2>
<%= form_for(:wo, :url => {:action => 'create'}) do |f| %>
<table>
<tr>
<th>Customer Name</th>
<td><%= f.text_field(:customer_id) %></td>
</tr>
<tr>
<th>Customer Contact Name</th>
<td><%= f.text_field(:customer_contact_name) %></td>
</tr>
<tr>
<th>Customer Contact Number</th>
<td><%= f.text_field(:customer_contact_email) %></td>
</tr>
<tr>
<th>Delivery Terms for Order</th>
<td><%= f.text_field(:delivery_terms_for_order) %></td>
</tr>
<tr>
<th>Customer PO Number</th>
<td><%= f.text_field(:customer_po_number) %></td>
</tr>
<tr>
<th>Sage Call Off Number</th>
<td><%= f.text_field(:sage_call_off_number) %></td>
</tr>
<tr>
<th>Order Detail 1</th>
<td><%= f.text_field(:order_detail_1) %></td>
</tr>
<tr>
<th>Order Details 2</th>
<td><%= f.text_field(:order_detail_2) %></td>
</tr>
<tr>
<th>Date Customer Ordered</th>
<td><%= f.date_field(:dt_customer_ordered) %></td>
</tr>
<tr>
<th>Date Customer Required</th>
<td><%= f.date_field(:dt_customer_required) %></td>
</tr>
<tr>
<th>Date Originally Promised</th>
<td><%= f.date_field(:dt_orig_promise) %></td>
</tr>
<tr>
<th>Current Fulfilment Date</th>
<td><%= f.date_field(:dt_current_fulfill) %></td>
</tr>
<tr>
<th>Sales Order Number</th>
<td><%= f.text_field(:sales_order_number) %></td>
</tr>
<tr>
<th>Sales Person</th>
<td><%= f.text_field(:sales_person) %></td>
</tr>
<tr>
<th>Customer Address</th>
<td><%= f.text_field(:customer_address_id) %></td>
</tr>
</table>
<div class="form-buttons">
<%= submit_tag("Create Work Order") %>
</div>
<% end %>
</div>
这是控制台上的输出
Started GET "/work_orders" for 127.0.0.1 at 2014-08-03 14:42:31 +0100
Processing by WorkOrdersController#index as HTML
WorkOrder Load (0.0ms) SELECT "work_orders".* FROM "work_orders"
Rendered work_orders/index.html.erb (7.0ms)
Completed 200 OK in 9ms (Views: 8.0ms | ActiveRecord: 0.0ms)
Started POST "/work_orders/create" for 127.0.0.1 at 2014-08-03 14:44:05 +0100
Processing by WorkOrdersController#create as HTML
Parameters: {"utf8"=>"V", "authenticity_token"=>"RSJo7m6eenJNOeF74O/hzfRdG6826XAQgVMGFtYPG/A=", "wo"=> {"customer_id"=>"", "
customer_contact_name"=>"Joe BLoggs", "customer_contact_email"=>"", "delivery_terms_for_order"=>"", "customer_po_number"=>"",
"sage_call_off_number"=>"", "order_detail_1"=>"", "order_detail_2"=>"",
"dt_customer_ordered"=>"", "dt_customer_required"=>"
", "dt_orig_promise"=>"", "dt_current_fulfill"=>"",
"sales_order_number"=>"", "sales_person"=>"", "customer_a
ddress_id"=>""}, "commit"=>"Create Work Order"}
Unpermitted parameters: utf8, authenticity_token, wo, commit
outputting varibles
(0.0ms) begin transaction
SQL (1.0ms) INSERT INTO "work_orders" ("created_at", "updated_at") VALUES (?, ?) [["created_at", "2014-08-03 13:44:05.618
638"], ["updated_at", "2014-08-03 13:44:05.618638"]]
(5.0ms) commit transaction
Redirected to http://localhost:3000/work_orders
Completed 302 Found in 26ms (ActiveRecord: 8.0ms)
有人能指出我正确的方向吗? /举个例子说明我应该做些什么?
此致
科林。
答案 0 :(得分:1)
params.permit()
前面应加一个params.require
- 表示表单正在包装的基础对象。您也可以在服务器输出的参数哈希中看到这一点:"wo"=> {"customer_id"=>"" # ... }
。因此,只需按照以下方式重新构建workorder_params
方法:
params.require(:wo)
.permit(:work_order_ref,
:customer_id,
# ...
)