Question

I have a strong parameter like this

params.require(:survey).permit( option_booleans_attributes: [:id, :survey_id, :topic, :answer])

if I use rails f.input I got parameter like

"option_booleans_attributes"=>{"0"=>{"answer"=>"true", "id"=>"5"}, "1"=>{"answer"=>"false", "id"=>"6"}, "2"=>{"answer"=>"true", "id"=>"7"}}}

but I need to show option_booleans topic and let user fill answer

  <% @survey.option_booleans.each do |question| %>
    <%= question.topic %><br>
    <%= radio_button "option_booleans_attributes[]", "answer[#{question.id}]", "true" %>是
    <%= radio_button "option_booleans_attributes[]", "answer[#{question.id}]", "false" %>否<br>
  <% end %>

But I don't know how to generate the 0 1 2 in the parameter..

about my survey.rb

class Survey < ActiveRecord::Base
has_many :option_booleans
accepts_nested_attributes_for :option_booleans

belongs_to :member

end

class OptionBoolean < ActiveRecord::Base
belongs_to :survey03

and OptionBoolean have topic:string and answer:boolean

I want to let user see the topic and update the answer

Was it helpful?

Solution 2

What I use now

   <li><% @survey03.option_booleans.each do |question| %>
   <%= question.topic %><br>
        <%= radio_button "option_booleans_attributes[#{question.id}]", "answer", "true", :required => true %>
        <%= radio_button "option_booleans_attributes[#{question.id}]", "answer", "false", :required => true %>
    <% end %></li>

and in controller

params[:option_booleans_attributes].each do |option_id, attributes|    

if option = OptionBoolean.find_by_id(option_id)
       option.update_attributes(attributes)
    end 
end

and I know if I want to generate index just use

    <% @survey.option_booleans.each_with_index do |question, index| %>        
      <%= question.topic %><br>
      <%= radio_button "option_booleans_attributes[#{index}]", "answer[#{question.id}]", "true" %>
      <%= radio_button "option_booleans_attributes[#{index}]", "answer[#{question.id}]", "false" %>
    <% end %>

OTHER TIPS

It seems you're missing several parts of your form:

#app/controllers/surveys_controller.rb
def new
   @survey = Survey.new
   @survey.option_booleans.build #-> creates ActiveRecord Object for your form
end

#app/views/surveys/new.html.erb
<%= form_for @survey do |f| %>
   <%= f.fields_for :option_booleans do |option| %>
       <% @survey.question_booleans.each do |question| %>
           <%= question.topic %>
           <%= f.radio_button "answer[#{question.id}]", "true" %>
           <%= f.radio_button "answer[#{question.id}]", "false" %>
       <% end %>
   <% end %>
<% end %>

Although I don't quite understand how your system works

You have a survey with many options, but you're passing new attributes to create new options each time. Surely you'd have option_booleans as a join model, with options as an independent model, and:

#app/models/survery.rb
Class Survey < ActiveRecord::Base
    has_many :option_booleans
    has_many :options, through: :option_booleans
end

try select options like this:

@variable_with_booleans = [true, false]
<%= f.select :param, @variable_with_booleans %>

answer to your latest comment: I don't sure, but try to edit your form

<%= form_for @question do |f| %>
  <%= f.label :topic %>
  <% @variable_with_booleans = [true, false] %>
  <%= f.select :answer, @variable_with_booleans %>
  <%= f.submit %>
<% end %>

and edit your strong params in controller,which contains this action

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top