I’m trying to create a custom partial for a has_many relationship and running into some issues.
My models
Message
has_many :formats
Format
belongs_to :message
validates_inclusion_of :format_type, :in => FORMAT_TYPES.keys
I have a constant “FORMAT_TYPES” (wmv, flv, etc…) so that each “Format” record has a message_id and a format_type string that is in the allowed list.
I’m trying to create a custom partial for rails_admin that allows the admin to use checkboxes to select which formats they want. Here’s what I’ve got:
- for format in FORMAT_TYPES.keys
%div
= check_box_tag "message[formats][]", format
= format
Which outputs this:
<fieldset>
<legend>Formats</legend>
<div>
<input id="message_formats_" name="message[formats][]" type="checkbox" value="640x360_8">
640x360_8
</div>
<div>
<input id="message_formats_" name="message[formats][]" type="checkbox" value="480x272_8">
480x272_8
</div>
...
</fieldset>
When I select a few formats and submit, I get this error:
ActiveRecord::AssociationTypeMismatch in RailsAdmin::MainController#create
Format(#2196273220) expected, got String(#2151941320)
This sounds like it’s expecting an existing Format id. Which makes me think I have to create a has_many_through and get rid of my constant. (trying to avoid that)
Trying to figure out how to properly format my partial to allow the creation of these new Format records. Any ideas?
Much thanks in advance!
The problem is that format is an object and you are passing in the string value of format. You can use format_ids instead of format.
In order to allow for no formats (if you want them to be able to save no formats), you will also need to add a dumby hidden field: