开发者

accepts_nested_attribute_for and multiple edits

Following on from this answer, I am having a spot of bother with accepts_nested_attributes_for.

First things first, this is my error code:

undefined method `reflect_on_association' for NilClass:Class line 39

which I get when calling this partial in _formfield.html.haml:

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 => productcode
42:   %br

I'm using a rails 3-ised, formtastic-ised version of Ryan Bates' Edit Multiple screencast. Here's my code:

edit_multiple.html.haml

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

index.html.haml (edited for relevance so if some of the haml formatting is out, it's probably not the error)

%td.main
  %h1
    ISBNs 
    %h6
  = semantic_form_for :edit_multiple_isbns, :url => 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"

isbns controller:

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

routes:

  resources :isbns do
    collection do
      post 'edit_multiple'
      put 'update_multiple'
    end 
  end

Isbn model:

  has_many    :productcodes, :dependent => :destroy
  accepts_nested_attributes_for :productcodes, :allow_destroy => true

And finally, _formfields.html.haml, the partial which is throwing the error:

%a{:name => "Identifiers"} 
%h6 Identifiers
#this works fine...
= f.inputs :name => "Identifier Information", :class => "main" do
  %li.tip
    = tooltip(:istc_id, :hover)
  = f.input :istc_id, :label_method => :title_title_text, :as => :select, :collection =>  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 => productcode
  %br

And here's its partial, _productcodes:

.nested-fields
  = f.inputs do
    %br
    %li.tip 
      = tooltip(:test, :hover)
    = f.input :idvalue, :label => "Product code"
    %br  
    %li.tip 
      = tooltip(:test, :hover)
    = f.input :idtype, :label => "Product code type", :as => :select, :collection => Identifier.all, :value_method => :code 
    %li.list
      = link_to "Edit list", identifiers_path      
    %br
    %li.tip
      .links
      = link_to_remove_association "Remove product code", f

Other answers have suggested changing the semantic_form_for :isbn to an instance variable, but in this case it throws:

undefined method `model_name' for NilClass:Class

Update//

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:

= semantic_form_for :isbn, :url => {:controller => 'isbns', :action => 'update_multiple'}, :html=>{:method=>: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 => ff

    = submit_tag "Submit"

But on submit I get this error:

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.

And the logs say:

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@rai开发者_StackOverflow中文版ls3tutorial/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'
  <internal:prelude>: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)

///////////// Update: Got it partially working, but not for nested models with pre-exising records prior to the update action, which throw:

Couldn't find Productcode with ID=13495 for Isbn with ID=3438

Isbns controller:

   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

Isbn model:

attr_accessible :isbns_attributes # weird, as this is the isbn model, but heck, it works.

_edit_multiple in the isbns view:

= semantic_form_for :isbn, @isbn, :url => {:controller => 'isbns', :action => 'update_multiple'}, :html=>{:method=>: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 => ff

    = submit_tag "Submit"


I think your problem lies in the edit.html.haml, which is not set correctly if i understand correctly.

edit_multiple.html.haml

= 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
        =f.fields_for 'isbns[]', isbn do |ff|
          = render 'formfields', f: ff

    = submit_tag "Submit"

Then you will have the isbn object available in your partials. If you follow my other answer exactly you can see that you are missing the fields_for part from the cycle. Railscast 198, but using formtastic

Let me know if it still doesn't work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜