【发布时间】:2014-06-04 13:36:52
【问题描述】:
我正在为多个多态模型、Trip 和 Service 创建一个评级表单,它们都公开了 Rateable mixin,它带来了一个关联 has_many :ratings。为了提交单个表单,我将两个多态模型合并为一个代理,然后遍历代理以使用 simple_fields_for 创建表单。
因为表单是针对 Rateable obj 上的 Rating 对象,simple_form 创建了一系列字段,但名称都是“ratings[value]”、“ratings[cmets]”等。为了解决这个问题,我使用了 @987654323 @参数我找到了here,修复了名称问题,如下图。但是标签也没有改变,而且我不知道如何告诉 SimpleForm 使用 index 参数来更改标签的 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>
我在 simple_fields_for 上添加了 index 参数,希望它能够更新标签和输入,但它似乎没有这样做。 如何更改标签指向的位置,使其指向具有新名称的输入
【问题讨论】: