Rails: radio button selection for nested objects -
i'm stuck simple selection task. have models:
# id :integer(4) not null, primary key # category :string(255) # content :text class question < activerecord::base has_many :choices, :dependent => :destroy accepts_nested_attributes_for :choices end # id :integer(4) not null, primary key # content :text # correct :boolean(1) # question_id :integer(4) class choice < activerecord::base belongs_to :question end when create new question, want specify in nested form not content of question, content of 3 answer objects, , select radio button 1 correct answer. in new action of controller, have this:
def new @title = "new question" @question = question.new 3.times { @question.choices.build } respond_to |format| format.html # new.html.erb format.xml { render :xml => @question } end end this form code:
<%= simple_form_for @question |question_form| %> <%= question_form.error_notification %> <div class="inputs"> <%= question_form.input :content, :label => 'question' %> <%= question_form.input :category, :collection => get_categories, :include_blank => false %> <% @question.choices.each |choice| %> <%= question_form.fields_for :choices, choice |choice_fields| %> <%= choice_fields.input :content, :label => 'choice' %> <%= choice_fields.radio_button :correct, true %> <%= choice_fields.label :correct, 'correct answer' %> <% end %> <% end %> </div> <div class="actions"> <%= question_form.button :submit %> </div> <% end %> the problem code produce 3 radio buttons different names: can select more 1 correct answer, , not correct behaviour. names of 3 radio buttons question[choices_attributes][0][correct], question[choices_attributes][1][correct] , question[choices_attributes][2][correct].
the question is: how can create 3 radio buttons same name, in order select 1 , 1 correct answer? how can create correct params array, in order save them in create action in way:
def create @question = question.new(params[:question]) # render or redirect stuff.... end thank much!
you can use radio_button_tag , pass own name attribute.
being still in choice_fields scope, have access few methods can expose object working on, name radio_button_tag properly.
the following code give radio button proper name accepted nested attribute:
<%= radio_button_tag "question[choices_attributes][#{choice_fields.index}][correct]", true, choice_fields.object.correct %>
choice_fields.index gives access proper index of resource being created, first coice have name question[choices_attributes][0][correct], second 1 question[choices_attributes][1][correct], etc.
choice_fields.object.correct gives access current value correct radio button filled in edit forms.
update: solution above wrong, gives each radio button different name attribute don't end working (you can select multiple radio buttons @ once).
the soltion ended going allong following lines:
<%= radio_button_tag "question[choices_attributes][correct_choice]", choice_fields.index, choice_fields.object.correct %> this gives each radio button same name others, , value equal index of correct choice. params hash ends looking this:
question: {choices_attributes: { "0": {//choice 0}, "1": {//choice 1}, etc... }, correct_choice: //index of correct answer } i updated controller manually update correct choice follows:
def create correct_index = params[:question][:correct_choice] params[:question][:choiceses_attributes][correct_index][:correct] = true @question = question.new(params[:question]) # render or redirect stuff.... end
Comments
Post a Comment