【问题标题】:Rails Form has blank fields. I need to allow this and save all the submitted dataRails 表单有空白字段。我需要允许并保存所有提交的数据
【发布时间】: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


【解决方案1】:

如果我是正确的,你的代码有错误。你的workorder_params方法应该是这样的

def workorder_params
  params.require(:work_order).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

注意work_order 不是wo

更新

另外,form 中的这一行

<%= form_for(:wo, :url => {:action => 'create'}) do |f| %>

应该是这样的

<%= form_for(@wo, :url => {:action => 'create'}) do |f| %>

【讨论】:

  • 感谢您抽出宝贵时间回复。当我进行更改时,我收到以下错误 ActionController::ParameterMissing in WorkOrdersController#create param is missing or the value is empty: work_order Extracted source (about line #48): private def workorder_params params.require(:work_order) .permit( :work_order_ref, Rails.rootv1 Application Trace | Framework Trace | Full Trace app/controllers/work_orders_controller.rb:48:in workorder_params' app/controllers/work_orders_controller.rb:22:in create' 请求参数:"wo"=>{"customer_id"=>"",
  • 我做了更改.. 但是记录没有保存任何数据.. sql 插入语句看起来没问题?
【解决方案2】:

找出进入 create 方法的参数,然后确保 def workout_params 实际上匹配进入的嵌套参数。在那个阶段,您还可以确保从表单中正确传递值检查参数。

所以回顾一下,将以下内容添加到您的创建方法的顶部

puts params
puts workout_params
#or use `binding.pry` if you have pry installed

并查看正在传递的值。

完成此操作后,如果您发现有什么问题,请修复它,或者将结果添加到您的问题中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-16
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    相关资源
    最近更新 更多