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!
I had a similar issue: I wanted to have a partial in a has_many
/ belongs_to
or has_and_belongs_to_many
association which would use checkboxes (checkbox group) instead of the default form widget.
I have made an extension (= custom view partial) to rails_admin
which allows to easily use checkbox group widget for the related models .
It is not exactly your use case (you have a kind of enumeration checkbox group) , but you could take inspiration from my template, like all templates in rails_admin
, even this mine is in HAML :
- selected_ids = (hdv = field.html_default_value).nil? ? selected_ids : hdv
- n = 3
- data = []
- all_values.sort {|x, y| x[0] <=> y[0] }.each_with_index do |item, index|
- (0..(n-1)).each do |p|
- data[p] ||= []
- data[p] << item if index % n == p
- data.each_with_index do |slice, c|
%div{:class => [:column, "col-#{c}"]}
- slice.each do |item|
%div.checkbox_field
= check_box_tag "#{form.dom_name(field)}", item[1], selected_ids.include?(item[1]), {:id => "#{field.method_name}_#{item[1]}"}
%label{:for => "#{field.method_name}_#{item[1]}"}
= item[0]
Other solution would be to use the default rails_admin
enumeration feature ( https://github.com/Juicymo/rails_admin/wiki/Enumeration ) and just change it's partial template to use checkboxes instead of adding a custom contant and forms.
In case of interest the template and the rails_admin
checkbox groups association widget extension is open-sourced on GitHub at: https://github.com/Juicymo/rails_admin/blob/master/app/views/rails_admin/main/_form_checkboxes_multiselect.html.haml
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.
= check_box_tag "message[format_ids][]", format.id
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:
= hidden_field_tag "message[format_ids][]", 0
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.