Simple_form: undefined method `macro' for f.association with mongoid (7.0.2)

Created on 8 Oct 2018  路  8Comments  路  Source: heartcombo/simple_form

Environment

  • Ruby 2.5.1p57
  • Rails 5.2.1
  • Simple Form 4.0.1
  • Mongoid 7.0.2

Current behavior

When using a form with Mongoid > 6 and using f.association :location, it shows this exception:

2018-10-08 12:41:45 +0100: Rack app error handling request { GET /agents/5bbb4279b4e4cf1fce975cf5/edit }
#<ActionView::Template::Error: undefined method `macro' for #<Mongoid::Association::Referenced::BelongsTo:0x00007fb47ce647a8>>
/usr/local/lib/ruby/gems/2.5.0/gems/simple_form-4.0.1/lib/simple_form/form_builder.rb:508:in `build_association_attribute'
/usr/local/lib/ruby/gems/2.5.0/gems/simple_form-4.0.1/lib/simple_form/form_builder.rb:220:in `association'
/Users/hackeron/Dropbox/Development/Xanview/timeline/app/views/agents/_form_minimal.html.slim:24:in `_app_views_agents__form_minimal_html_slim__3949601813411023705_70206448617520'
/usr/local/lib/ruby/gems/2.5.0/gems/actionview-5.2.1/lib/action_view/template.rb:159:in `block in render'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/notifications.rb:170:in `instrument'
/usr/local/lib/ruby/gems/2.5.0/gems/actionview-5.2.1/lib/action_view/template.rb:354:in `instrument_render_template'
/usr/local/lib/ruby/gems/2.5.0/gems/actionview-5.2.1/lib/action_view/template.rb:157:in `render'
/usr/local/lib/ruby/gems/2.5.0/gems/actionview-5.2.1/lib/action_view/renderer/partial_renderer.rb:344:in `block in render_partial'
/usr/local/lib/ruby/gems/2.5.0/gems/actionview-5.2.1/lib/action_view/renderer/abstract_renderer.rb:44:in `block in instrument'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/notifications.rb:168:in `block in instrument'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/notifications/instrumenter.rb:23:in `instrument'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/notifications.rb:168:in `instrument'

The code in question is:

          = f.association :location, collection: @locations, \
              include_blank: "No Location", \
              prompt: t('simple_form.placeholders.location_selection')

Expected behavior

It should show the form correctly as it does with Mongoid 6.x

Maybe a similar change to this needs to be done? < https://github.com/sferik/rails_admin/commit/9ef623f6cba73adbf86833d9eb07f1be3924a133

Most helpful comment

I just changed f.association :model to f.input :model_id and it worked

All 8 comments

+1
We receive the same error (rails 5.2.1, simple_form 4.0.1, mongoid 7.0.2, ruby 2.3.8).

Thank you for the issue. We never supported mongoid officially. It was working mostly by coincidence and if it breaks we don't promise we will fix.

This information is on the readme:

Please note that the association helper is currently only tested with Active Record. It currently does not work well with Mongoid and depending on the ORM you're using your mileage may vary.

:( anyone has any workaround for this?

Since there is no workaround and I've had other issues and unexpected behaviour with simple_form, I am biting the bullet and rewriting 53 forms from simple_form_for to form_for.

Thank you simple_form for all these years of making things easier for so many of us, it's been great!

I just changed f.association :model to f.input :model_id and it worked

Can confirm the solution by @chrisokamoto also works with multiple selects:
f.input :category_ids, collection: Category.all, input_html: {multiple: true}

Also worth noting, when you do something like this your Strong Params will need to be updated, and they are particular about how you construct them with nested arrays.

See this article for more info: Permitting Nested Arrays using Strong Params in Rails

I just changed f.association :model to f.input :model_id and it worked

Can also confirm that it works but you must change your ActiveModel validations accordingly (from model to model_id) in order to maintain the error handling facilities provided by simple_form.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mameier picture mameier  路  3Comments

gmrash picture gmrash  路  3Comments

yshmarov picture yshmarov  路  4Comments

piya23300 picture piya23300  路  5Comments

joshm1204 picture joshm1204  路  3Comments