I am creating a ratings form for a number of polymorphic models, Trip and Service, both of which expose the Rateable mixin, which brings in an association has_many :ratings
. To submit a single form, I conglomerate both polymorphic models into a proxy, then iterate over the proxy to create the form with simple_fields_for.
Because the forms are for Rating objects on Rateable obj, simple_form created a series of fields, but the names are all "ratings[value]", "ratings[comments]" etc. To fix this, I used the index
parameter I found here, which fixed the name issue, as shown below. The labels didn't change as well, though, and I don't know how to tell SimpleForm to use the index
param to change the label's for
attribute.
RatingsProxy.rb
attr_reader :rateables # A simple array of different types of Models
end
module Rateable
has_many :ratings, as: :rateable
accepts_nested_attributes_for :ratings
end
= simple_form_for @ratings_proxy do |f|
- @ratings_proxy.rateables.each do |rateable|
= simple_fields_for :ratings, rateable.ratings.build, index: rateable.class.name do |ratings_form|
= ratings_form.input :value, as: :radio_buttons
= ratings_form.input :comments
Now, my HTML for the inputs looks like this:
<!-- Created by one rateable model, Trip. Note the name attribute on the label vs. input -->
<label for="ratings_Trip_value_5" name="ratings[value]">
<input class="radio_buttons required" id="ratings_Trip_value_5" name="ratings[Trip][value]" type="radio" value="5">
5
</label>
...
<!-- Created by a different rateable model, Service -->
<label for="ratings_value_5" name="ratings[value]">
<input class="radio_buttons required" id="ratings_Service_value_5" name="ratings[Service][value]" type="radio" value="5">
5
</label>
I added the index
param on the simple_fields_for expecting it to update labels as well as inputs, but it doesn't seem to have done so. How do I change where the label points, so that it points at the inputs with the new name