Polymorphic association in Simpleform

ruby, ruby-on-rails, ruby-on-rails-4

Solution

Through much hem, hawing, and back and forth, we've determined that SimpleForm does not do this.

Here's why! (Well, probably why)

SimpleForm needs to figure out what class the association is for. Since the default case is that the association name is the de-capitalized name of the class, it ends up looking for the class "Chattable", and doesn't find it, which is where your error comes from.

Good news is that all you need to do is replace the `f.association :chattable` line with something that does what you need. http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease has the info you need to do this "easy way" - aka, with the Rails form helpers.

My suggestion is to have a selection box for `chattable_type`, and some JS that un-hides the HTML for the selection box of that type. So you'd get something like

= select_tag(:chattable_type, ["Booth", "Venue"])

= collection_for_select(:chattable_id, Booth.all)
= collection_for_select(:chattable_id, Venue.all)
...

not including the JS and CSS. Check the docs linked above for the actual syntax; I think mine's a bit off.

Problem

Is there a way to display polymorphic association in `simple_form` view? So far I've got below: ``` = simple_form_for(@chat, :html => { :class => "form-horizontal" }, :wrapper => "horizontal", defaults: { :input_html => { class: "form-control"}, label_html: { class: "col-lg-4" } } ) do |f| = f.error_notification .form-inputs = f.association :from_user = f.association :to_user = f.input :message = f.association :chattable .form-actions = f.button :submit ``` And below model: ``` class Chat < ActiveRecord::Base belongs_to :from_user, :foreign_key => 'from_user_id', class_name: 'User' belongs_to :to_user, :foreign_key => 'to_user_id', class_name: 'User' belongs_to :chattable, polymorphic: true validates :from_user, associated: true, presence: true validates :message, presence: true end ``` This throws out below error: ``` uninitialized constant Chat::Chattable ```

Original source