Passionate Engineer
Passionate Engineer

Reputation: 10422

Polymorphic association in Simpleform

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

Upvotes: 10

Views: 5141

Answers (2)

mingle
mingle

Reputation: 1617

I found other solution that does not require JS manipulation and can still use simple form input. You can use input select with id and type separated by comma passed as option value.

= f.input :chattable, collection: @chat.chattables, selected: f.object.chattable.try(:signature),

Then in Chat model:

  def chattables
    PolymorphicModel.your_condition.map {|t| [t.name, t.signature] }
  end

  def chattable=(attribute)
    self.chattable_id, self.chattable_type = attribute.split(',')
  end

And in your PylymorphicModel

  def signature
    [id, type].join(",")
  end

Remember to add chattable to secured params if you use them.

Upvotes: 14

Narfanator
Narfanator

Reputation: 5813

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.

Upvotes: 5

Related Questions