简体   繁体   中英

Assigning ruby on rails Posts to Blog categories (via simple_form gem)

I'm trying to link associate blog posts with categories through the following simple_form where the label_method: :name and value_method: :id are both pointing to the blog_categories table values:

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

The form works fine without applying the blog categories, but it raises an error when I load the posts/new page: nil is not a symbol nor a string

BlogCategory has_many :posts | Post belongs_to :blog_category

My posts and blog_categories tables:

create_table "posts", force: :cascade do |t|
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "title"
  t.text "body"
  t.string "summary"
  t.string "thumbnail_link"
  t.integer "category_id"
end

create_table "blog_categories", force: :cascade do |t|
  t.string "name"
  t.integer "parent_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

Full trace:

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'

The way I'm trying to populate the selector dropdown via Posts_controller's new method: @categories = BlogCategory.all

I'm not quite sure if I am doing this properly. If so, what is the best approach to this?

The error is in this line:

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

The following solutions might work for you. Replace the line with any of the solutions:

<%= f.association :category %>

<%= f.input :category_id, collection: BlogCategory.all, value_method: :id, label_method: :name %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM