Ruby on rails 如何更改简单字段中的标签名称属性
我正在为多个多态模型Trip和Service创建一个评级表,这两个模型都公开了Rateable mixin,它带来了一个关联Ruby on rails 如何更改简单字段中的标签名称属性,ruby-on-rails,simple-form,Ruby On Rails,Simple Form,我正在为多个多态模型Trip和Service创建一个评级表,这两个模型都公开了Rateable mixin,它带来了一个关联有很多:评级。为了提交一个表单,我将两个多态模型合并到一个代理中,然后在代理上迭代以创建带有简单字段的表单 由于表单用于Rateable obj上的评级对象,因此simple_表单创建了一系列字段,但名称都是“ratings[value]”、“ratings[comments]”等。为了解决这个问题,我使用了我找到的index参数,修复了名称问题,如下所示。但是标签也没有改
有很多:评级。为了提交一个表单,我将两个多态模型合并到一个代理中,然后在代理上迭代以创建带有简单字段的表单
由于表单用于Rateable obj上的评级对象,因此simple_表单创建了一系列字段,但名称都是“ratings[value]”、“ratings[comments]”等。为了解决这个问题,我使用了我找到的index
参数,修复了名称问题,如下所示。但是标签也没有改变,我不知道如何告诉SimpleForm使用索引
参数来更改标签的for
属性
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
现在,输入的HTML如下所示:
<!-- 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>
5.
...
5.
我在simple_字段上添加了index
参数,以期望它更新标签和输入,但它似乎没有这样做如何更改标签指向的位置,使其指向具有新名称的输入我最终解决了这个问题,将评级形式.input:value,as::单选按钮
替换为评级形式.collection\u单选按钮
,作为后续操作,我从未在simpleform中找到控制标签的for
属性的方法