Ruby on rails 将RubyonRails文章分配到博客类别(通过简单的表单gem)

Ruby on rails 将RubyonRails文章分配到博客类别(通过简单的表单gem),ruby-on-rails,ruby,simple-form,Ruby On Rails,Ruby,Simple Form,我试图通过以下simple\u表单将博客文章与类别关联起来,label\u方法::name和value\u方法::id都指向blog\u类别表值: <div class="container"> <div class="row"> <div class="col-xs-12"> <%= render 'shared/exitBlocker' %> <h1 class="page-section">New Blog P

我试图通过以下
simple\u表单
将博客文章与类别关联起来,
label\u方法::name
value\u方法::id
都指向
blog\u类别
表值:

<div class="container">
<div class="row">
  <div class="col-xs-12">
    <%= render 'shared/exitBlocker' %>
    <h1 class="page-section">New Blog Post</h1>
    <div class="wellington center news-form">
      <%= simple_form_for @post, url: posts_path do |f| %>
        <%= render 'shared/error_messages', object: f.object %>
        <%= f.input :title, input_html: { maxlength: 51 }, class: "form-control center" %>
        <%= f.input :summary, input_html: { maxlength: 300 }, class: "form-control center" %>
        <%= f.input :category_id, collection: @categories, as: :grouped_select, label_method: :name, value_method: :id , input_html: { class: "form-control center" } %>
        <%= f.input :thumbnail_link, class: "form-control center" %>
        <%= f.input :body, :as => :ckeditor, input_html: {:ckeditor => {:toolbar => 'FULL'}}, class: "form-control left" %>
        <%= f.submit "Post", id: "saveChange", class: "btn btn-success btn-block" %>
      <% end %>
    </div>
  </div>
</div><!-- Content row -->
</div><!-- Content container -->
完整跟踪:

actionview (5.1.5) lib/action_view/helpers/form_options_helper.rb:458:in `block in option_groups_from_collection_for_select'
actionview (5.1.5) lib/action_view/helpers/form_options_helper.rb:456:in `map'
actionview (5.1.5) lib/action_view/helpers/form_options_helper.rb:456:in `option_groups_from_collection_for_select'
actionview (5.1.5) lib/action_view/helpers/tags/grouped_collection_select.rb:23:in `render'
actionview (5.1.5) lib/action_view/helpers/form_options_helper.rb:258:in `grouped_collection_select'
actionview (5.1.5) lib/action_view/helpers/form_options_helper.rb:841:in `grouped_collection_select'
simple_form (3.5.1) lib/simple_form/inputs/grouped_collection_select_input.rb:10:in `input'
simple_form (3.5.1) lib/simple_form/wrappers/leaf.rb:20:in `call'
simple_form (3.5.1) lib/simple_form/wrappers/leaf.rb:20:in `render'
simple_form (3.5.1) lib/simple_form/wrappers/many.rb:29:in `block in render'
simple_form (3.5.1) lib/simple_form/wrappers/many.rb:27:in `each'
simple_form (3.5.1) lib/simple_form/wrappers/many.rb:27:in `render'
simple_form (3.5.1) lib/simple_form/wrappers/root.rb:16:in `render'
simple_form (3.5.1) lib/simple_form/form_builder.rb:123:in `input'
app/views/posts/new.html.erb:11:in `block in _app_views_posts_new_html_erb__847991209_125594352'
actionview (5.1.5) lib/action_view/helpers/capture_helper.rb:39:in `block in capture'
actionview (5.1.5) lib/action_view/helpers/capture_helper.rb:203:in `with_output_buffer'
actionview (5.1.5) lib/action_view/helpers/capture_helper.rb:39:in `capture'
actionview (5.1.5) lib/action_view/helpers/form_helper.rb:450:in `form_for'
simple_form (3.5.1) lib/simple_form/action_view_extensions/form_helper.rb:27:in `block in simple_form_for'
simple_form (3.5.1) lib/simple_form/action_view_extensions/form_helper.rb:46:in `with_simple_form_field_error_proc'
simple_form (3.5.1) lib/simple_form/action_view_extensions/form_helper.rb:26:in `simple_form_for'
app/views/posts/new.html.erb:7:in `_app_views_posts_new_html_erb__847991209_125594352'
actionview (5.1.5) lib/action_view/template.rb:157:in `block in render'
activesupport (5.1.5) lib/active_support/notifications.rb:168:in `instrument'
actionview (5.1.5) lib/action_view/template.rb:352:in `instrument_render_template'
actionview (5.1.5) lib/action_view/template.rb:155:in `render'
actionview (5.1.5) lib/action_view/renderer/template_renderer.rb:52:in `block (2 levels) in render_template'
actionview (5.1.5) lib/action_view/renderer/abstract_renderer.rb:42:in `block in instrument'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `instrument'
actionview (5.1.5) lib/action_view/renderer/abstract_renderer.rb:41:in `instrument'
actionview (5.1.5) lib/action_view/renderer/template_renderer.rb:51:in `block in render_template'
actionview (5.1.5) lib/action_view/renderer/template_renderer.rb:59:in `render_with_layout'
actionview (5.1.5) lib/action_view/renderer/template_renderer.rb:50:in `render_template'
actionview (5.1.5) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (5.1.5) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (5.1.5) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (5.1.5) lib/action_view/rendering.rb:103:in `_render_template'
actionpack (5.1.5) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (5.1.5) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (5.1.5) lib/action_controller/metal/rendering.rb:52:in `render_to_body'
actionpack (5.1.5) lib/action_controller/metal/renderers.rb:141:in `render_to_body'
actionpack (5.1.5) lib/abstract_controller/rendering.rb:24:in `render'
actionpack (5.1.5) lib/action_controller/metal/rendering.rb:36:in `render'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (5.1.5) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
C:/RailsInstaller/Ruby2.3.0/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'
activesupport (5.1.5) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (5.1.5) lib/active_record/railties/controller_runtime.rb:29:in `cleanup_view_runtime'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:43:in `render'
meta-tags (2.8.0) lib/meta_tags/controller_helper.rb:20:in `render'
actionpack (5.1.5) lib/action_controller/metal/implicit_render.rb:33:in `default_render'
actionpack (5.1.5) lib/action_controller/metal/basic_implicit_render.rb:4:in `block in send_action'
actionpack (5.1.5) lib/action_controller/metal/basic_implicit_render.rb:4:in `tap'
actionpack (5.1.5) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.5) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.5) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.5) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.5) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.5) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:844:in `call'
rack (2.0.4) lib/rack/etag.rb:25:in `call'
rack (2.0.4) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.4) lib/rack/head.rb:12:in `call'
rack (2.0.4) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.4) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.5) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.5) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.5) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.5) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.5) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.5) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.4) lib/rack/method_override.rb:22:in `call'
rack (2.0.4) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.4) lib/rack/sendfile.rb:111:in `call'
railties (5.1.5) lib/rails/engine.rb:522:in `call'
puma (3.11.3) lib/puma/configuration.rb:225:in `call'
puma (3.11.3) lib/puma/server.rb:624:in `handle_request'
puma (3.11.3) lib/puma/server.rb:438:in `process_client'
puma (3.11.3) lib/puma/server.rb:302:in `block in run'
puma (3.11.3) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
我试图通过Posts\u controller的
new
方法填充选择器下拉列表的方式:
@categories=BlogCategory.all


我不太确定我是否做得很好。如果是这样,最好的解决方法是什么?

错误在这一行:

<%= f.input :category_id, collection: @categories, as: :grouped_select, label_method: :name, value_method: :id , input_html: { class: "form-control center" } %>

以下解决方案可能适合您。使用以下任一解决方案更换管路:



你能粘贴错误日志以便知道哪一行正抛出错误吗?@Kartikeytana当然,浏览器页面的完整跟踪?是的,这会有帮助。另外,添加
posts/new.html.erb
文件的所有内容。@kartikytana在那里,现在应该都在那里了。谢谢你的邀请@Kartikeytana我还提供了一些关于我通过simple_表单集合(在问题的顶部)调用哪些数据的更多信息。谢谢,它成功了!有没有办法根据父项id将下拉列表中列出的值分隔开?@Jake您是如何定义父项类别的?在另一个模型中?它实际上是通过
blog\u category
模型:
有很多:sub\u blog\u categories,class\u name:“category”,foreign\u key::parent\u id
来消除对
:subcategory
表/列的需求。如果您想了解更多信息,我会通过找到此链接。此链接有效吗?否,出现一个错误,说明:
未初始化常量BlogCategory::Category
<%= f.input :category_id, collection: @categories, as: :grouped_select, label_method: :name, value_method: :id , input_html: { class: "form-control center" } %>
<%= f.input :category_id, collection: BlogCategory.all, value_method: :id, label_method: :name %>