【发布时间】:2014-08-03 16:38:45
【问题描述】:
你好,我有一个表格,可以在数据库中创建一个 enw 条目。如果用户没有完成整个表单,则会创建一条记录,但输入的任何数据都不会保存到该记录中。
我希望了解如何允许创建方法接受并非所有字段都将完成并且只会保存它拥有的字段。
谁能帮忙?
我的控制器代码如下所示:
def create
@wo = WorkOrder.new(workorder_params)
puts "outputting varibles"
if @wo.save
redirect_to(:action => 'index')
else
render('new')
end
end
private
def workorder_params
params.require(:wo)
.permit(
: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
有人能指出我正确的方向并用我的代码给我一个例子吗?
表格代码为:
<%= 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 %>
修改代码后,表单正确发布,但数据未保存.. 控制台输出如下所示
Started POST "/work_orders/create" for 127.0.0.1 at 2014-08-03 18:51:54 +0100
Processing by WorkOrdersController#create as HTML
Parameters: {"utf8"=>"V", "authenticity_token"=>"RSJo7m6eenJNOeF74O/hzfRdG6826XAQgVMGFtYPG/A=", "work_order"=> {"customer_id
"=>"", "customer_contact_name"=>"joe", "customer_contact_email"=>"6786786786", "delivery_terms_for_order"=>"", "customer_po_n
umber"=>"", "sage_call_off_number"=>"", "order_detail_1"=>"", "order_detail_2"=>"", "dt_customer_ordered"=>"", "dt_customer_r
equired"=>"", "dt_orig_promise"=>"", "dt_current_fulfill"=>"", "sales_order_number"=>"", "sales_person"=>"",
"customer_address_id"=>""}, "commit"=>"Create Work Order"}
outputting varibles
(0.0ms) begin transaction
SQL (2.0ms) INSERT INTO "work_orders" ("created_at", "customer_contact_email", "customer_contact_name", "customer_po_numbe
r", "delivery_terms_for_order", "order_detail_1", "order_detail_2", "sage_call_off_number", "sales_order_number", "sales_pers
on", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["created_at", "2014-08- 03 17:51:54.856110"], ["customer_contac
t_email", "6786786786"], ["customer_contact_name", "joe"], ["customer_po_number", ""], ["delivery_terms_for_order", ""], ["or
der_detail_1", ""], ["order_detail_2", ""], ["sage_call_off_number", ""], ["sales_order_number", ""], ["sales_person", ""], [
"updated_at", "2014-08-03 17:51:54.856110"]]
(5.0ms) commit transaction
Redirected to http://localhost:3000/work_orders
Completed 302 Found in 15ms (ActiveRecord: 7.0ms)
使用 .save! ---(网页上没有显示错误)
Started POST "/work_orders/create" for 127.0.0.1 at 2014-08-03 19:56:13 +0100
Processing by WorkOrdersController#create as HTML
Parameters: {"utf8"=>"V", "authenticity_token"=>"RSJo7m6eenJNOeF74O/hzfRdG6826XAQgVMGFtYPG/A=", "work_order"=> {"customer_id
"=>"", "customer_contact_name"=>"joe", "customer_contact_email"=>"6786786786", "delivery_terms_for_order"=>"", "customer_po_n
umber"=>"", "sage_call_off_number"=>"", "order_detail_1"=>"", "order_detail_2"=>"", "dt_customer_ordered"=>"", "dt_customer_r
equired"=>"", "dt_orig_promise"=>"", "dt_current_fulfill"=>"", "sales_order_number"=>"", "sales_person"=>"",
"customer_address_id"=>""}, "commit"=>"Create Work Order"}
outputting varibles
(0.0ms) 开始交易 SQL (2.0ms) INSERT INTO "work_orders" ("created_at", "customer_contact_email", "customer_contact_name", "customer_po_numbe r”、“delivery_terms_for_order”、“order_detail_1”、“order_detail_2”、“sage_call_off_number”、“sales_order_number”、“sales_pers” on", "updated_at") 值 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) [["created_at", "2014-08- 03 18:56:13.821830"] , ["customer_contac t_email"、"6786786786"]、["customer_contact_name"、"joe"]、["customer_po_number"、""]、["delivery_terms_for_order"、""]、["或 der_detail_1", ""], ["order_detail_2", ""], ["sage_call_off_number", ""], ["sales_order_number", ""], ["sales_person", ""], [ "updated_at", "2014-08-03 18:56:13.821830"]] (10.0ms) 提交事务 重定向到 /work_orders Completed 302 Found in 31ms (ActiveRecord: 14.0ms)
Started GET "/work_orders" for 127.0.0.1 at 2014-08-03 19:56:13 +0100
Processing by WorkOrdersController#index as HTML
WorkOrder Load (1.0ms) SELECT "work_orders".* FROM "work_orders"
Rendered work_orders/index.html.erb (6.0ms)
Completed 200 OK in 10ms (Views: 8.0ms | ActiveRecord: 1.0ms)
【问题讨论】:
-
你能告诉我们你的
form code吗? -
你能发布你生成的
server log吗? -
我更新了我的答案。它现在应该可以工作了。
-
您的
models中有任何validations设置吗?试试if @wo.save!而不是if @wo.save并告诉我出现的任何验证错误。 -
顺便说一句,你应该看看 simple_form gem
标签: ruby-on-rails forms validation