Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving multiple records with params.require in ruby
    primarykey
    data
    text
    <p>I'm trying to update multiple records. The form generates fine and submits fine and the data is sent. The records are looked up to update, but no data is ever saved. I have my controller logic, form logic and console dump below. I'm trying to duplicate what Anthony Lewis <a href="http://anthonylewis.com/2011/04/15/editing-multiple-records-in-rails/" rel="nofollow">put together</a> but I have a feeling I am not passing the right data into or defining correctly the <code>params.require().permit()</code> method. Thanks in advance for your help!</p> <pre><code>class ConfigController &lt; ApplicationController def edit @services = Service.all end def update params["service"].keys.each do |id| @service = Service.find(id.to_i) @service.update_attributes!(service_params) end redirect_to config_url end private def service_params params.require(:service).permit(:id, :client_id, :client_secret) end end </code></pre> <p>Form code is:</p> <pre><code> &lt;%= form_for :service, :url =&gt; update_config_path, :html =&gt; { :class =&gt; "form-horizontal", :method =&gt; "put", :remote =&gt; true } do %&gt; &lt;% @services.each do |s| %&gt; &lt;%= fields_for "service[]", s do |service_field| %&gt; &lt;fieldset&gt; &lt;legend&gt;&lt;%= s.name %&gt;&lt;/legend&gt; &lt;div class="form-group"&gt; &lt;%= service_field.label :client_id, "Consumer Key", :class =&gt; "col-sm-2 control-label" %&gt; &lt;div class="col-sm-10"&gt; &lt;%= service_field.text_field :client_id, :class =&gt; "form-control" %&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="form-group"&gt; &lt;%= service_field.label :client_secret, "Consumer Secret", :class =&gt; "col-sm-2 control-label" %&gt; &lt;div class="col-sm-10"&gt; &lt;%= service_field.text_field :client_secret, :class =&gt; "form-control" %&gt; &lt;/div&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;%= submit_tag %&gt; &lt;% end %&gt; </code></pre> <p>Console reads:</p> <pre><code>Started PUT "/config" for 127.0.0.1 at 2013-11-22 15:44:08 -0800 Processing by ConfigController#update as JS Parameters: {"utf8"=&gt;"✓", "service"=&gt;{"1"=&gt;{"client_id"=&gt;"testid", "client_secret"=&gt;"testsecret"}, "2"=&gt;{"client_id"=&gt;"testkey", "client_secret"=&gt;""}, "3"=&gt;{"client_id"=&gt;"", "client_secret"=&gt;""}}, "commit"=&gt;"Save changes"} Service Load (0.3ms) SELECT "services".* FROM "services" WHERE "services"."id" = $1 LIMIT 1 [["id", 1]] Unpermitted parameters: 1, 2, 3 (0.1ms) BEGIN (0.1ms) COMMIT Unpermitted parameters: 1, 2, 3 {} Service Load (0.2ms) SELECT "services".* FROM "services" WHERE "services"."id" = $1 LIMIT 1 [["id", 2]] Unpermitted parameters: 1, 2, 3 (0.1ms) BEGIN (0.1ms) COMMIT Unpermitted parameters: 1, 2, 3 {} Service Load (0.2ms) SELECT "services".* FROM "services" WHERE "services"."id" = $1 LIMIT 1 [["id", 3]] Unpermitted parameters: 1, 2, 3 (0.1ms) BEGIN (0.1ms) COMMIT Unpermitted parameters: 1, 2, 3 {} Redirected to http://localhost:3000/config Completed 302 Found in 6ms (ActiveRecord: 1.1ms) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload