Ruby on Rails Saturday, June 24, 2017

sendAnswers page is a survey page with questions and options.

    def sendAnswers
@questions = @homework.questions

The view for sendAnswers is

    = form_for :survey do |f|
- @questions.each_with_index do |q, i|
= "#{i+1})"
= q.question
- if q.options.size == 0
= f.text_field 'answer_' +, placeholder: 'Answer', class: 'form-control'
- else
- q.options.each do |o|
= f.radio_button 'answer_' +, o.option
= o.option
= f.submit "Submit", class: 'btn btn-primary', data: {confirm: 'Are you sure you want to submit this homework?'}

The page posts to the following method which saves the answers to questions in database. 

def saveAnswers
@homework.questions.each do |q|
.answer = params[:survey]['answer_' +]
.question = q
.user = current_user
.homework = @homework

As you can see above i am appending question ids to the questions in the view page to track which answer belongs to which question and in the save action i use the appended question ids to save the answer to respective question. I am wondering whether there is a better and more natural way of binding this and not doing it manually like i am doing. Can this be achieved using form objects? I appreciate any guide to this dilemma. Thanks! 

You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To post to this group, send email to
To view this discussion on the web visit
For more options, visit

No comments:

Post a Comment