我正在尝试使用active_admin实现具有多对多关联(使用:through)的表单。我收到未定义的方法`call'for nil:NilClass 错误消息。
我是ruby和rails的新手。
我的模特:
article.rb
class Article < ActiveRecord::Base
has_many :tagged_articles
has_many :tags, :through => :tagged_articles, :source => :tag, :class_name => TaggedArticle
accepts_nested_attributes_for :tags
end
tagged_article.rb
class TaggedArticle < ActiveRecord::Base
belongs_to :article
belongs_to :tag
end
tag.rb
class Tag < ActiveRecord::Base
has_many :tagged_articles
has_many :articles, :through => :tagged_articles
end
视图
_form.html
<%= semantic_form_for @article, :html => {:id => "new_article",
:novalidate => "novalidate"} do |f| %>
app / admin / article.rb (active_admin文件)
ActiveAdmin.register Article do
form partial: 'form'
form do |f|
f.has_many :tags, heading: 'Tags', allow_destroy: true, new_record: true
f.actions
end
show do
# renders app/views/admin/posts/_some_partial.html.erb
render 'preview'
end
#permit_params :title, :text,
# tags_attributes: [:name]
permit_params do
permitted = [:title, :text,
tags_attributes: [:name]]
# permitted << :other if resource.something?
permitted
end
end
我收到此错误(打开表单时)
应用程序跟踪
Extracted source (around line #1):
1
insert_tag renderer_for(:new)
app/admin/article.rb:6:in `block (2 levels) in <top (required)>'
完整跟踪
Application Trace | Framework Trace | Full Trace
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/form_builder.rb:61:in `block in has_many'
formtastic (2.3.0.rc2) lib/formtastic/helpers/inputs_helper.rb:375:in `call'
formtastic (2.3.0.rc2) lib/formtastic/helpers/inputs_helper.rb:375:in `block (2 levels) in inputs_for_nested_attributes'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/form_builder.rb:143:in `block in field_set_and_list_wrapping'
actionpack (4.0.2) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionpack (4.0.2) lib/action_view/helpers/capture_helper.rb:200:in `with_output_buffer'
actionpack (4.0.2) lib/action_view/helpers/capture_helper.rb:38:in `capture'
formtastic (2.3.0.rc2) lib/formtastic/helpers/fieldset_wrapper.rb:32:in `field_set_and_list_wrapping'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/form_builder.rb:142:in `field_set_and_list_wrapping'
formtastic (2.3.0.rc2) lib/formtastic/helpers/inputs_helper.rb:292:in `inputs'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/form_builder.rb:20:in `block in inputs'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/form_builder.rb:151:in `with_new_form_buffer'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/form_builder.rb:20:in `inputs'
formtastic (2.3.0.rc2) lib/formtastic/helpers/inputs_helper.rb:373:in `block in inputs_for_nested_attributes'
actionpack (4.0.2) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionpack (4.0.2) lib/action_view/helpers/capture_helper.rb:200:in `with_output_buffer'
actionpack (4.0.2) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionpack (4.0.2) lib/action_view/helpers/form_helper.rb:1864:in `block in fields_for_nested_model'
actionpack (4.0.2) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionpack (4.0.2) lib/action_view/helpers/capture_helper.rb:200:in `with_output_buffer'
actionpack (4.0.2) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionpack (4.0.2) lib/action_view/helpers/form_helper.rb:699:in `fields_for'
actionpack (4.0.2) lib/action_view/helpers/form_helper.rb:1863:in `fields_for_nested_model'
actionpack (4.0.2) lib/action_view/helpers/form_helper.rb:1849:in `block in fields_for_with_nested_attributes'
actionpack (4.0.2) lib/action_view/helpers/form_helper.rb:1847:in `each'
actionpack (4.0.2) lib/action_view/helpers/form_helper.rb:1847:in `fields_for_with_nested_attributes'
actionpack (4.0.2) lib/action_view/helpers/form_helper.rb:1521:in `fields_for'
formtastic (2.3.0.rc2) lib/formtastic/helpers/inputs_helper.rb:391:in `inputs_for_nested_attributes'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/form_builder.rb:176:in `block in js_for_has_many'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/form_builder.rb:151:in `with_new_form_buffer'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/form_builder.rb:176:in `js_for_has_many'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/form_builder.rb:99:in `block in has_many'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/form_builder.rb:160:in `block in without_wrapper'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/form_builder.rb:151:in `with_new_form_buffer'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/form_builder.rb:160:in `without_wrapper'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/form_builder.rb:90:in `has_many'
app/admin/article.rb:6:in `block (2 levels) in <top (required)>'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/views/pages/form.rb:23:in `instance_exec'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/views/pages/form.rb:23:in `block in main_content'
actionpack (4.0.2) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionpack (4.0.2) lib/action_view/helpers/capture_helper.rb:200:in `with_output_buffer'
actionpack (4.0.2) lib/action_view/helpers/capture_helper.rb:38:in `capture'
actionpack (4.0.2) lib/action_view/helpers/form_helper.rb:435:in `form_for'
formtastic (2.3.0.rc2) lib/formtastic/helpers/form_helper.rb:167:in `block in semantic_form_for'
formtastic (2.3.0.rc2) lib/formtastic/helpers/form_helper.rb:190:in `with_custom_field_error_proc'
formtastic (2.3.0.rc2) lib/formtastic/helpers/form_helper.rb:166:in `semantic_form_for'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/view_helpers/form_helper.rb:9:in `active_admin_form_for'
arbre (1.0.1) lib/arbre/element.rb:175:in `method_missing'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/views/pages/form.rb:22:in `main_content'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/views/pages/base.rb:83:in `block (2 levels) in build_main_content_wrapper'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:31:in `block in build_tag'
arbre (1.0.1) lib/arbre/context.rb:92:in `with_current_arbre_element'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:49:in `with_current_arbre_element'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:26:in `build_tag'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:39:in `insert_tag'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:14:in `div'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/views/pages/base.rb:82:in `block in build_main_content_wrapper'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:31:in `block in build_tag'
arbre (1.0.1) lib/arbre/context.rb:92:in `with_current_arbre_element'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:49:in `with_current_arbre_element'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:26:in `build_tag'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:39:in `insert_tag'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:14:in `div'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/views/pages/base.rb:81:in `build_main_content_wrapper'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/views/pages/base.rb:65:in `block in build_page_content'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:31:in `block in build_tag'
arbre (1.0.1) lib/arbre/context.rb:92:in `with_current_arbre_element'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:49:in `with_current_arbre_element'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:26:in `build_tag'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:39:in `insert_tag'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:14:in `div'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/views/pages/base.rb:64:in `build_page_content'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/views/pages/base.rb:47:in `block (2 levels) in build_page'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:31:in `block in build_tag'
arbre (1.0.1) lib/arbre/context.rb:92:in `with_current_arbre_element'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:49:in `with_current_arbre_element'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:26:in `build_tag'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:39:in `insert_tag'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:14:in `div'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/views/pages/base.rb:44:in `block in build_page'
arbre (1.0.1) lib/arbre/context.rb:92:in `with_current_arbre_element'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:49:in `with_current_arbre_element'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/views/pages/base.rb:43:in `build_page'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/lib/active_admin/views/pages/base.rb:10:in `build'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:30:in `block in build_tag'
arbre (1.0.1) lib/arbre/context.rb:92:in `with_current_arbre_element'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:26:in `build_tag'
arbre (1.0.1) lib/arbre/element/builder_methods.rb:39:in `insert_tag'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/app/views/active_admin/resource/new.html.arb:1:in `block in __home_javkhlan__rvm_gems_ruby_______bundler_gems_active_admin__ca___f__ab__app_views_active_admin_resource_new_html_arb__4076692881198465766_51912660'
arbre (1.0.1) lib/arbre/context.rb:45:in `instance_eval'
arbre (1.0.1) lib/arbre/context.rb:45:in `initialize'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/app/views/active_admin/resource/new.html.arb:1:in `new'
/home/javkhlan/.rvm/gems/ruby-2.1.0/bundler/gems/active_admin-9ca081f47ab4/app/views/active_admin/resource/new.html.arb:1:in `__home_javkhlan__rvm_gems_ruby_______bundler_gems_active_admin__ca___f__ab__app_views_active_admin_resource_new_html_arb__4076692881198465766_51912660'
actionpack (4.0.2) lib/action_view/template.rb:143:in `block in render'
activesupport (4.0.2) lib/active_support/notifications.rb:161:in `instrument'
actionpack (4.0.2) lib/action_view/template.rb:141:in `render'
actionpack (4.0.2) lib/action_view/renderer/template_renderer.rb:49:in `block (2 levels) in render_template'
actionpack (4.0.2) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
activesupport (4.0.2) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.2) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.2) lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
actionpack (4.0.2) lib/action_view/renderer/template_renderer.rb:48:in `block in render_template'
actionpack (4.0.2) lib/action_view/renderer/template_renderer.rb:56:in `render_with_layout'
actionpack (4.0.2) lib/action_view/renderer/template_renderer.rb:47:in `render_template'
actionpack (4.0.2) lib/action_view/renderer/template_renderer.rb:17:in `render'
actionpack (4.0.2) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionpack (4.0.2) lib/action_view/renderer/renderer.rb:23:in `render'
actionpack (4.0.2) lib/abstract_controller/rendering.rb:127:in `_render_template'
actionpack (4.0.2) lib/action_controller/metal/streaming.rb:219:in `_render_template'
actionpack (4.0.2) lib/abstract_controller/rendering.rb:120:in `render_to_body'
actionpack (4.0.2) lib/action_controller/metal/rendering.rb:33:in `render_to_body'
actionpack (4.0.2) lib/action_controller/metal/renderers.rb:26:in `render_to_body'
actionpack (4.0.2) lib/abstract_controller/rendering.rb:97:in `render'
actionpack (4.0.2) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.0.2) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/home/javkhlan/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.0.2) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:41:in `block in render'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:84:in `cleanup_view_runtime'
activerecord (4.0.2) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:40:in `render'
actionpack (4.0.2) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.0.2) lib/action_controller/metal/responder.rb:233:in `default_render'
actionpack (4.0.2) lib/action_controller/metal/responder.rb:161:in `to_html'
responders (1.0.0) lib/responders/flash_responder.rb:104:in `to_html'
actionpack (4.0.2) lib/action_controller/metal/responder.rb:154:in `respond'
actionpack (4.0.2) lib/action_controller/metal/responder.rb:147:in `call'
actionpack (4.0.2) lib/action_controller/metal/mime_responds.rb:330:in `respond_with'
inherited_resources (1.4.1) lib/inherited_resources/actions.rb:19:in `new'
actionpack (4.0.2) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.0.2) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.0.2) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (4.0.2) lib/active_support/callbacks.rb:433:in `_run__1569366334490291269__process_action__callbacks'
activesupport (4.0.2) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.2) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.0.2) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.0.2) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.0.2) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.0.2) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.0.2) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (4.0.2) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.0.2) lib/abstract_controller/base.rb:136:in `process'
actionpack (4.0.2) lib/abstract_controller/rendering.rb:44:in `process'
actionpack (4.0.2) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.0.2) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.0.2) lib/action_controller/metal.rb:231:in `block in action'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:80:in `call'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:48:in `call'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:71:in `block in call'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.0.2) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.0.2) lib/action_dispatch/routing/route_set.rb:680:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:25:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/flash.rb:241:in `call'
rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/cookies.rb:486:in `call'
activerecord (4.0.2) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.0.2) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
activerecord (4.0.2) lib/active_record/migration.rb:369:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.0.2) lib/active_support/callbacks.rb:373:in `_run__782737588198479971__call__callbacks'
activesupport (4.0.2) lib/active_support/callbacks.rb:80:in `run_callbacks'
actionpack (4.0.2) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/reloader.rb:64:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.2) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.2) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.0.2) lib/rails/engine.rb:511:in `call'
railties (4.0.2) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/home/javkhlan/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/home/javkhlan/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/home/javkhlan/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
关于如何使其发挥作用的任何想法?
答案 0 :(得分:2)
我认为nilclass错误的原因是嵌套表单没有指定的字段。
添加
f.has_many :tags do |cf|
cf.input :tag, :label => "Tag"
end
解决了nilclass问题
最后,以下代码有效。在这种特殊情况下,没有必要使用_form.html.erb。
form do |f|
f.has_many :tags do |cf|
cf.input :tag, :label => "Tag"
end
f.actions
end
答案 1 :(得分:0)
如果您在表单字段中使用allow_destroy: true
,则还需要在accepts_nested_attributes_for
上使用它。
class Article < ActiveRecord::Base
has_many :tagged_articles
has_many :tags, :through => :tagged_articles, :source => :tag, :class_name => TaggedArticle
accepts_nested_attributes_for :tags, allow_destroy: true
end