title: accepts_nested_attribute_for and multiple edits
body: <p>Following on from this <a href="https://stackoverflow.com/questions/6541224/railscast-198-but-using-formtastic/6611752#6611752">answer</a>, I am having a spot of bother with accepts_nested_attributes_for.</p>
  
  <p>First things first, this is my error code: </p>
  
  <pre><code>undefined method `reflect_on_association' for NilClass:Class line 39
  </code></pre>
  
  <p>which I get when calling this partial in _formfield.html.haml:</p>
  
  <pre><code>36:     = link_to "Edit list", notifications_path
  37:   %br
  38:   %li.tip
  39:     = link_to_add_association 'Add product codes', f, :productcodes
  40:     = f.semantic_fields_for :productcodes do |productcode|
  41:       = render 'productcode_fields', :f =&gt; productcode
  42:   %br
  </code></pre>
  
  <p>I'm using a rails 3-ised, formtastic-ised version of Ryan Bates' <a href="http://railscasts.com/episodes/165-edit-multiple" rel="nofollow noreferrer">Edit Multiple</a> screencast. Here's my code: </p>
  
  <p>edit_multiple.html.haml</p>
  
  <pre><code>= semantic_form_for :isbn, url: { controller: 'isbns', action: 'update_multiple' }, html: { method: :put } do |f|
    %ul
      - @isbns.each do |isbn|
        %li
          = hidden_field_tag "isbn_ids[]", isbn.id
          = isbn.descriptivedetail_titledetail_titleelement_titlewithoutprefix
      = render 'formfields', f: f
      = submit_tag "Submit"
  </code></pre>
  
  <p>index.html.haml (edited for relevance so if some of the haml formatting is out, it's probably not the error)</p>
  
  <pre><code>%td.main
    %h1
      ISBNs 
      %h6
    = semantic_form_for :edit_multiple_isbns, :url =&gt; edit_multiple_isbns_path do
      %table
        = will_paginate     
        %tr
          %th
            = sortable "title", "Title"      
        - @isbns.each do |isbn|
          %tr
            %td
              = check_box_tag "isbn_ids[]", isbn.id
            %td
              = link_to isbn.title, isbn
      %table
        %tr
          = select_tag :field, options_for_select([["All Fields", ""], ["Name", "name"], ["Price", "price"], ["Category", "category_id"], ["Discontinued", "discontinued"]])
          = submit_tag "Edit Checked"
  </code></pre>
  
  <p>isbns controller: </p>
  
  <pre><code>def edit_multiple
    @isbns = Isbn.find(params[:isbn_ids])
  end    
  
  def update_multiple
    @isbns = Isbn.find(params[:isbn_ids])
    @isbns.each do |isbn|
      isbn.update_attributes!(params[:isbn])
    end
  
    flash[:notice] = "Updated titles!"
    redirect_to isbns_path
  end
  </code></pre>
  
  <p>routes:</p>
  
  <pre><code>  resources :isbns do
      collection do
        post 'edit_multiple'
        put 'update_multiple'
      end 
    end
  </code></pre>
  
  <p>Isbn model: </p>
  
  <pre><code>  has_many    :productcodes, :dependent =&gt; :destroy
    accepts_nested_attributes_for :productcodes, :allow_destroy =&gt; true
  </code></pre>
  
  <p>And finally, _formfields.html.haml, the partial which is throwing the error: </p>
  
  <pre><code>%a{:name =&gt; "Identifiers"} 
  %h6 Identifiers
  #this works fine...
  = f.inputs :name =&gt; "Identifier Information", :class =&gt; "main" do
    %li.tip
      = tooltip(:istc_id, :hover)
    = f.input :istc_id, :label_method =&gt; :title_title_text, :as =&gt; :select, :collection =&gt;  Istc.all
  #this nested one throws the error... 
    %br
    %li.tip
      = link_to_add_association 'Add product codes', f, :productcodes
      = f.semantic_fields_for :productcodes do |productcode|
        = render 'productcode_fields', :f =&gt; productcode
    %br
  </code></pre>
  
  <p>And here's its partial, _productcodes:</p>
  
  <pre><code>.nested-fields
    = f.inputs do
      %br
      %li.tip 
        = tooltip(:test, :hover)
      = f.input :idvalue, :label =&gt; "Product code"
      %br  
      %li.tip 
        = tooltip(:test, :hover)
      = f.input :idtype, :label =&gt; "Product code type", :as =&gt; :select, :collection =&gt; Identifier.all, :value_method =&gt; :code 
      %li.list
        = link_to "Edit list", identifiers_path      
      %br
      %li.tip
        .links
        = link_to_remove_association "Remove product code", f
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/1556944/render-partial-from-another-model">Other answers</a> have suggested changing the <code>semantic_form_for :isbn</code> to an instance variable, but in this case it throws: </p>
  
  <pre><code>undefined method `model_name' for NilClass:Class
  </code></pre>
  
  <p>Update//</p>
  
  <p>The code in the answer below succumbs to haml's rules and produces one form for each record on the edit_multiple show. This is resolved with this tweak: </p>
  
  <pre><code>= semantic_form_for :isbn, :url =&gt; {:controller =&gt; 'isbns', :action =&gt; 'update_multiple'}, :html=&gt;{:method=&gt;:put} do |f|
    %ul
      - for isbn in @isbns
        %li
          = hidden_field_tag "isbn_ids[]", isbn.id
          = isbn.descriptivedetail_titledetail_titleelement_titlewithoutprefix
      = f.fields_for 'isbns[]', isbn do |ff|
        = render 'formfields', :f =&gt; ff
  
      = submit_tag "Submit"
  </code></pre>
  
  <p>But on submit I get this error: </p>
  
  <pre><code>500 Internal Server Error
  
  If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong.
  </code></pre>
  
  <p>And the logs say: </p>
  
  <pre><code>Error during failsafe response: ActionView::Template::Error
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/activesupport-3.0.6/lib/active_support/whiny_nil.rb:48:in `method_missing'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/actionpack-3.0.6/lib/action_dispatch/http/parameters.rb:10:in `parameters'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb:3:in `___sers__mma__rvm_gems_ruby_______p____rails_tutorial_gems_actionpack_______lib_action_dispatch_middleware_templates_rescues_diagnostics_erb___945168515264926955_2203247360__4560118438262045809'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/actionpack-3.0.6/lib/action_view/template.rb:135:in `block in render'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/activesupport-3.0.6/lib/active_support/notifications.rb:54:in `instrument'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/actionpack-3.0.6/lib/action_view/template.rb:127:in `render'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/actionpack-3.0.6/lib/action_view/render/rendering.rb:59:in `block in _render_template'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/activesupport-3.0.6/lib/active_support/notifications.rb:52:in `block in instrument'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/activesupport-3.0.6/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/activesupport-3.0.6/lib/active_support/notifications.rb:52:in `instrument'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/actionpack-3.0.6/lib/action_view/render/rendering.rb:56:in `_render_template'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/actionpack-3.0.6/lib/action_view/render/rendering.rb:26:in `render'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/haml-3.1.2/lib/haml/helpers/action_view_mods.rb:13:in `render_with_haml'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/actionpack-3.0.6/lib/action_dispatch/middleware/show_exceptions.rb:88:in `rescue_action_locally'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/actionpack-3.0.6/lib/action_dispatch/middleware/show_exceptions.rb:68:in `render_exception'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/actionpack-3.0.6/lib/action_dispatch/middleware/show_exceptions.rb:59:in `call'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/railties-3.0.6/lib/rails/rack/logger.rb:13:in `call'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/rack-1.2.3/lib/rack/runtime.rb:17:in `call'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/activesupport-3.0.6/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/rack-1.2.3/lib/rack/lock.rb:11:in `block in call'
    &lt;internal:prelude&gt;:10:in `synchronize'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/rack-1.2.3/lib/rack/lock.rb:11:in `call'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/actionpack-3.0.6/lib/action_dispatch/middleware/static.rb:30:in `call'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/railties-3.0.6/lib/rails/application.rb:168:in `call'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/railties-3.0.6/lib/rails/application.rb:77:in `method_missing'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/railties-3.0.6/lib/rails/rack/log_tailer.rb:14:in `call'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/rack-1.2.3/lib/rack/content_length.rb:13:in `call'
    /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/rack-1.2.3/lib/rack/handler/webrick.rb:52:in `service'
    /Users/Emma/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
    /Users/Emma/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
    /Users/Emma/.rvm/rubies/ruby-1.9.2-p180/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
  
  
  Started POST "/isbns/update_multiple" for 127.0.0.1 at 2011-07-08 13:01:12 +0100
  
  TypeError (expected Array (got Hash) for param `isbns'):
  
  
  Rendered /Users/Emma/.rvm/gems/ruby-1.9.2-p180@rails3tutorial/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (0.8ms)
  </code></pre>
  
  <p>/////////////
  Update: Got it partially working, but not for nested models with pre-exising records prior to the update action, which throw: </p>
  
  <pre><code>Couldn't find Productcode with ID=13495 for Isbn with ID=3438
  </code></pre>
  
  <p>Isbns controller: </p>
  
  <pre><code>   def update_multiple
       @isbns = Isbn.find(params[:isbn_ids])
       @isbns.each do |isbn|
         isbn.send :attributes=, params[:isbn][:isbns], false
         isbn.save!
       end
       flash[:notice] = "Updated titles!"
       redirect_to isbns_path
     end
  </code></pre>
  
  <p>Isbn model: </p>
  
  <p>attr_accessible  :isbns_attributes # weird, as this is the isbn model, but heck, it works. </p>
  
  <p>_edit_multiple in the isbns view: </p>
  
  <pre><code>= semantic_form_for :isbn, @isbn, :url =&gt; {:controller =&gt; 'isbns', :action =&gt; 'update_multiple'}, :html=&gt;{:method=&gt;:put} do |f|
    %ul
      - for isbn in @isbns
        %li
          = hidden_field_tag "isbn_ids[]", isbn.id
          = isbn.descriptivedetail_titledetail_titleelement_titlewithoutprefix
      = f.fields_for 'isbns', isbn do |ff|
        = render 'formfields', :f =&gt; ff
  
      = submit_tag "Submit"
  </code></pre>