從這個answer繼續,我有一個麻煩的地方accept_nested_attributes_for。第一accepted_nested_attribute_for和多個編輯
第一件事,這是我的錯誤代碼:
undefined method `reflect_on_association' for NilClass:Class line 39
調用此部分中_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
我使用的是軌道的3-是瑞安貝茨'Edit Multiple screencast的formtastic版本。這裏是我的代碼:
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(編輯的相關性,所以如果一些HAML格式的都出了,這可能不是錯誤)
%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"
的ISBN控制器:
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
路線:
resources :isbns do
collection do
post 'edit_multiple'
put 'update_multiple'
end
end
書號型號:
has_many :productcodes, :dependent => :destroy
accepts_nested_attributes_for :productcodes, :allow_destroy => true
最後,_formfields.html.haml,部分被投擲的錯誤:
%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
下面是它的部分,_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已建議更改semantic_form_for :isbn
實例變量,但在這種情況下,它會拋出:
undefined method `model_name' for NilClass:Class
更新//
在下面的答案代碼屈從於HAML的規則,併產生一個形式在edit_multiple顯示每個記錄。這解決了與這個調整:
= 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"
但在提交我得到這個錯誤:
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.
,並將日誌說:
Error during failsafe response: ActionView::Template::Error
/Users/Emma/.rvm/gems/[email protected]/gems/activesupport-3.0.6/lib/active_support/whiny_nil.rb:48:in `method_missing'
/Users/Emma/.rvm/gems/[email protected]/gems/actionpack-3.0.6/lib/action_dispatch/http/parameters.rb:10:in `parameters'
/Users/Emma/.rvm/gems/[email protected]/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/[email protected]/gems/actionpack-3.0.6/lib/action_view/template.rb:135:in `block in render'
/Users/Emma/.rvm/gems/[email protected]/gems/activesupport-3.0.6/lib/active_support/notifications.rb:54:in `instrument'
/Users/Emma/.rvm/gems/[email protected]/gems/actionpack-3.0.6/lib/action_view/template.rb:127:in `render'
/Users/Emma/.rvm/gems/[email protected]/gems/actionpack-3.0.6/lib/action_view/render/rendering.rb:59:in `block in _render_template'
/Users/Emma/.rvm/gems/[email protected]/gems/activesupport-3.0.6/lib/active_support/notifications.rb:52:in `block in instrument'
/Users/Emma/.rvm/gems/[email protected]/gems/activesupport-3.0.6/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/Emma/.rvm/gems/[email protected]/gems/activesupport-3.0.6/lib/active_support/notifications.rb:52:in `instrument'
/Users/Emma/.rvm/gems/[email protected]/gems/actionpack-3.0.6/lib/action_view/render/rendering.rb:56:in `_render_template'
/Users/Emma/.rvm/gems/[email protected]/gems/actionpack-3.0.6/lib/action_view/render/rendering.rb:26:in `render'
/Users/Emma/.rvm/gems/[email protected]/gems/haml-3.1.2/lib/haml/helpers/action_view_mods.rb:13:in `render_with_haml'
/Users/Emma/.rvm/gems/[email protected]/gems/actionpack-3.0.6/lib/action_dispatch/middleware/show_exceptions.rb:88:in `rescue_action_locally'
/Users/Emma/.rvm/gems/[email protected]/gems/actionpack-3.0.6/lib/action_dispatch/middleware/show_exceptions.rb:68:in `render_exception'
/Users/Emma/.rvm/gems/[email protected]/gems/actionpack-3.0.6/lib/action_dispatch/middleware/show_exceptions.rb:59:in `call'
/Users/Emma/.rvm/gems/[email protected]/gems/railties-3.0.6/lib/rails/rack/logger.rb:13:in `call'
/Users/Emma/.rvm/gems/[email protected]/gems/rack-1.2.3/lib/rack/runtime.rb:17:in `call'
/Users/Emma/.rvm/gems/[email protected]/gems/activesupport-3.0.6/lib/active_support/cache/strategy/local_cache.rb:72:in `call'
/Users/Emma/.rvm/gems/[email protected]/gems/rack-1.2.3/lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
/Users/Emma/.rvm/gems/[email protected]/gems/rack-1.2.3/lib/rack/lock.rb:11:in `call'
/Users/Emma/.rvm/gems/[email protected]/gems/actionpack-3.0.6/lib/action_dispatch/middleware/static.rb:30:in `call'
/Users/Emma/.rvm/gems/[email protected]/gems/railties-3.0.6/lib/rails/application.rb:168:in `call'
/Users/Emma/.rvm/gems/[email protected]/gems/railties-3.0.6/lib/rails/application.rb:77:in `method_missing'
/Users/Emma/.rvm/gems/[email protected]/gems/railties-3.0.6/lib/rails/rack/log_tailer.rb:14:in `call'
/Users/Emma/.rvm/gems/[email protected]/gems/rack-1.2.3/lib/rack/content_length.rb:13:in `call'
/Users/Emma/.rvm/gems/[email protected]/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/[email protected]/gems/actionpack-3.0.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (0.8ms)
///////// //// 更新:得到它部分工作,但不適用於更新操作之前的預先存在記錄的嵌套模型,它會拋出:
Couldn't find Productcode with ID=13495 for Isbn with ID=3438
的ISBN控制器:
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
書號型號:
attr_accessible:isbns_attributes#奇怪,因爲這是ISBN模式,但心裏很不舒服,它的工作原理。
_edit_multiple中的ISBN查看:
= 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"
嗨 - 謝謝! 此代碼爲所選的每個標題在一個頁面上顯示單獨的表單。有一點修補haml格式讓我們到所有標題的一種形式,但然後 - 有趣 - 在提交我得到一個500內部服務器錯誤。我會在清晰的問題中發佈代碼和錯誤...並再次感謝。 – snowangel
另外,呃,抱歉沒有按照您的超清晰指示解決其他問題。 – snowangel
進步的位,我認爲:我可以通過在數組中放入索引 - isbns [1]而不是isbns []來獲取表單。 但是現在在堆棧中,我得到了警告:無法批量分配受保護的屬性:isbns,這很奇怪,因爲isbns本身就是這樣。 attr_accessible:isbns_attributes和attr_writer:isbns沒有區別。我將調查虛擬屬性或其他東西... – snowangel