user2105143
user2105143

Reputation: 45

RoR v4 Strong Params Issues

Having a little problem with Strong Params (I think) though this could be a "layer 8" issue :-)

I have a form that passes data to a create method. The data appear in the server console however data is not save to the table. Looking at the server output I see there is the following error / statement.: Unpermitted parameters: utf8, authenticity_token, wo, commit

my create method looks like this:

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

my form looks like this

<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>

And this is the output on the console

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)

Can anyone point me in the right direction? / give me an example of what i should be doing?

Regards

Colin.

Upvotes: 1

Views: 41

Answers (1)

pdobb
pdobb

Reputation: 18037

The params.permit() should be preceded by a params.require -- which indicates the base object that the form is wrapping. You can see this in the parameters hash in your server output as well: "wo"=> {"customer_id"=>"" # ... }. So just restructure your workorder_params method like this:

params.require(:wo)
      .permit(:work_order_ref,
              :customer_id,
              # ...
             )

Upvotes: 1

Related Questions