[英]How to make simple_form do what I want with accepts_nested_attributes_for
Each survey
has_many
results
, and accepts_nested_attributes_for
results
. 每个
survey
has_many
results
,并accepts_nested_attributes_for
results
。
/app/models/survey.rb: /app/models/survey.rb:
attr_accessible :results_attributes
has_many :results
accepts_nested_attributes_for :results
Each result is either selected or not selected: 每个结果都已选中或未选中:
/db/schema.rb: /db/schema.rb:
create_table "results", :force => true do |t|
t.boolean "selected", :default => false
t.integer "survey_id"
end
Sometimes @survey.additive == true
. 有时
@survey.additive == true
。 In those cases, any number of results can be selected. 在这些情况下,可以选择任意数量的结果。 You'd represent this with a series of checkboxes, one for each
result
in @survey.results
. 您可以通过一系列复选框来表示这一点,
@survey.results
每个result
一个。
Sometimes @survey.additive == false
. 有时
@survey.additive == false
。 In those cases, only one result can be selected. 在这些情况下,只能选择一个结果。 You'd represent this with a series of radio buttons, one for each
result
in @survey.results
. 您可以使用一系列单选按钮来表示这一点,
@survey.results
每个result
一个。
I am using simple_form and HAML. 我正在使用simple_form和HAML。
I'm trying to write /app/views/surveys/_form.html.haml
so that it will create a series of radio buttons to choose which results are selected when @survey.additive == false
. 我正在尝试编写
/app/views/surveys/_form.html.haml
以便它将创建一系列单选按钮,以选择@survey.additive == false
时选择的结果。
(From there it should be easy to extrapolate the code to also create a series of checkboxes when @survey.additive == true
.) (从那里可以容易地推断出代码,以在
@survey.additive == true
时创建一系列复选框。)
My first attempt was: 我的第一次尝试是:
= simple_form_for [@competitor, @survey] do |f|
= f.error_notification
= f.simple_fields_for :results do |r|
= r.input :selected, :as => :radio_buttons, :label => r.name
= f.button :submit, :id => 'submit_survey'
That earned me an undefined method 'name' for #<SimpleForm::FormBuilder:0x007fbd1f687048>
error. 这
undefined method 'name' for #<SimpleForm::FormBuilder:0x007fbd1f687048>
错误赢得了一个undefined method 'name' for #<SimpleForm::FormBuilder:0x007fbd1f687048>
。
My second move was removing :label => r.name
, but the result was four separate rows of radio buttons, each row labelled "Selected" and each containing a "Yes" radio button and a "No" radio button. 我的第二个步骤是删除
:label => r.name
,但结果是四行单选按钮,每行标记为“已选择”,每行包含一个“是”单选按钮和一个“否”单选按钮。 By separate I mean that selecting Yes in one row deselected No in that row but had no affect on the radio buttons in the other rows. 分开表示,是指在一行中选择“是”会取消选择该行中的“否”,但对其他行中的单选按钮没有影响。 (What I actually needed was four connected radio buttons, each labelled after with a
result.name
in @survey.results
, so that deselecting any of the four would deselect the other three.) (我实际上需要的是四个连接的单选按钮,每个按钮在
@survey.results
带有一个result.name
标记,以便取消选择这四个按钮中的任何一个都将取消选择其他三个按钮。)
After that, I tried many variations, but so far all my attempts have failed. 之后,我尝试了许多变体,但到目前为止,我的所有尝试都失败了。 What should I do?
我该怎么办?
The underfined method 'name'
error can be overcome by using r.object.name
. underfined method 'name'
错误可以通过使用r.object.name
来克服。
If you can have many results with selected
set to true, this will suffice and it should work in the situation described above. 如果将
selected
设置为true可以得到很多结果,那么就足够了,并且应该可以在上述情况下工作。 If not, please read on. 如果没有,请继续阅读。
The thing with radio_buttons on a boolean is that they will generate a yes
and no
option. 布尔值上带有radio_buttons的东西是它们将生成
yes
和no
选项。 If you only want one selected result, I suggest switching to a situation where Survey has_many :results
and belongs_to :selected_result
. 如果只希望选择一个结果,建议切换到Survey
has_many :results
和belongs_to :selected_result
。 Then you'll be able to build a set of radio buttons for setting the selected_result_id
, generated from survey.results
. 然后,你就可以建立一套单选按钮来设置
selected_result_id
,从产生survey.results
。
If that's not an option, you will have to validate Survey with a custom validation so that it can only have one selected_result (add a scope for that) and build a set of radio buttons generated from survey.results
for an attr_accessor :selected_result_id
. 如果不是这样,则必须使用自定义验证来验证Survey,以便它只能具有一个selected_result(为此添加一个范围),并为
attr_accessor :selected_result_id
构建一个由survey.results
生成的单选按钮。 Then, in your controller, you will have to iterate through the results and set result.selected = false
on every result except the selected one. 然后,在控制器中,您将必须遍历结果并为除选定结果以外的所有结果设置
result.selected = false
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.