简体   繁体   中英

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 .

/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:

create_table "results", :force => true do |t|
  t.boolean  "selected", :default => false
  t.integer "survey_id"
end

Sometimes @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 .

Sometimes @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 .

I am using simple_form and 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 .

(From there it should be easy to extrapolate the code to also create a series of checkboxes when @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.

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. 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.)

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 .

If you can have many results with selected set to true, this will suffice and it should work in the situation described above. If not, please read on.

The thing with radio_buttons on a boolean is that they will generate a yes and no option. If you only want one selected result, I suggest switching to a situation where Survey has_many :results and 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 .

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 . Then, in your controller, you will have to iterate through the results and set result.selected = false on every result except the selected one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM