继续answer之后,我对accept_nested_attributes_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-ised,formtastic-eded版本的Ryan Bates'Edit Multiple截屏视频。这是我的代码:
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"
isbns控制器:
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
Isbn模特:
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 show上的每条记录生成一个表单。通过此调整解决了这个问题:
= 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/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'
<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)
///////////// 更新:部分工作,但不适用于在更新操作之前具有预先存在记录的嵌套模型,这会抛出:
Couldn't find Productcode with ID=13495 for Isbn with ID=3438
Isbns控制器:
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模特:
attr_accessible:isbns_attributes#怪异,因为这是isbn模型,但是,它确实有效。
isbns视图中的_edit_multiple:
= 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"
答案 0 :(得分:1)
我认为您的问题在于edit.html.haml,如果我理解正确,则设置不正确。
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"
然后您将在部分中使用isbn对象。
如果您完全按照我的其他答案,您可以看到您错过了周期中的fields_for
部分。
Railscast 198, but using formtastic
如果它仍然不起作用,请告诉我。