简体   繁体   English

如何使simple_form与accepts_nested_attributes_for一起做什么

[英]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的东西是它们将生成yesno选项。 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 :resultsbelongs_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.

相关问题 Simple_form和accepts_nested_attributes_for的问题 - Issues with Simple_form and accepts_nested_attributes_for 如何使用accepts_nested_attributes_for? - How do I use accepts_nested_attributes_for? Rails 3,多对多形式使用accepts_nested_attributes_for,如何正确设置? - Rails 3, many-to-many form using accepts_nested_attributes_for, how do I set up correctly? Rails accepts_nested_attributes_for-简单表单表单 - Rails accepts_nested_attributes_for - Simple Forms form 使用accepts_nested_attributes_for对表单进行了哪些更改 - What changes were done in a form using accepts_nested_attributes_for 如何使用accepts_nested_attributes_for手动构建正确的嵌套表单? - How to manually build a correct nested form with accepts_nested_attributes_for? 在没有accepts_nested_attributes_for的情况下,如何在Rails中更新嵌套属性? - How do I update Nested Attributes in Rails without accepts_nested_attributes_for? 如何使simple_form呈现我的嵌套属性 - How to make simple_form present my nested attributes accepts_nested_attributes_for:我做错了什么 - accepts_nested_attributes_for: What am I doing wrong 在Rails中,如何使用accepts_nested_attributes_for创建嵌套对象? - In Rails, how do I create nested objects using accepts_nested_attributes_for?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM