0

I have a Track that has_many Quizzes and a Quiz has_many Questions.

quiz.rb:

class Quiz < ActiveRecord::Base
  belongs_to :track
  has_many :questions, dependent: :destroy
  accepts_nested_attributes_for :questions
end

question.rb:

class Question < ActiveRecord::Base
  belongs_to :quiz
end

quizzes_controller.rb:

def new
  @quiz = Quiz.new
  @track = Track.find(params[:permalink])
  @course = Course.find(@track.course_id)

  3.times { @quiz.questions.build }
end

def create
  @track = Track.find(params[:permalink])
  @quiz = @track.quizzes.build(quiz_params)
  @course = Course.find(@track.course_id)
  if @quiz.save
    flash[:success] = 'Quiz successfully created'
    redirect_to quiz_path @quiz
  else
    redirect_to track_path @track
  end
end

private

def quiz_params
  params.require(:quiz).permit(:name, :information, :order, 
                               :permalink, :user_id,
                               questions_attributes: [:id, :content])
end

params on submit:

{"utf8"=>"✓",
 "authenticity_token"=>"...",
 "quiz"=> {
   "name"=>"Test Quiz",
   "questions_attributes"=> {
     "0"=>{"content"=>"This is question 1?"},
     "1"=>{"content"=>"This is question 2?"},
     "2"=>{"content"=>""}
   }, "user_id"=>"1",
   "order"=>"3"
 }, "commit"=>"Submit",
 "permalink"=>"1-basics"}

When I press submit the quiz is created but the questions are not created.

New params:

def quiz_params
    params.require(:quiz).permit(:name, :information, :order, 
                                 :permalink, :user_id, 
                                 questions_attributes: [:id, :content, :_destroy, answers_attributes: [:id, :content, :_destroy]] )
  end
Tim
  • 3,191
  • 2
  • 16
  • 22

2 Answers2

1

I think the problem is on accepts_nested_attributes_for:

reject_if: proc { |q| q['name'].blank? }

You probably should reject instances if content is blank (considering request parameters):

reject_if: proc { |q| q['content'].blank? }
markets
  • 6,709
  • 1
  • 38
  • 48
  • Removing that completely makes no difference, the error was still happening before that was added. – Tim Feb 25 '14 at 19:36
  • 1
    Not possible man, I still think your problem was reject of questions with blank name, it make sense since you don't permit name in questions_attributes: [:id, :content], and instances were rejected (Tested Rails 4.0.2). – markets Feb 25 '14 at 21:04
0

Problem solved. The nested parameters need to be wrapped in curly brackets:

def quiz_params
  params.require(:quiz).permit(:name, :information, :order, 
                               :permalink, :user_id,
                               { questions_attributes: [:id, :content] })
end
Tim
  • 3,191
  • 2
  • 16
  • 22
  • I think this was not the cause, but `reject_if` option was rejecting all questions because the blank names. Unless you have some initializer or callback changing `name` attribute. – markets Feb 25 '14 at 21:10
  • @markets can't be, as I already said the error still happens when that is removed. – Tim Feb 25 '14 at 21:21
  • Could you please try it again? Restart the server before the request. Anyway I believe it would be better if you add `reject_if: proc { |q| q['content'].blank? }` (or some model validation). I recreated the test in Rails 4.0.2 and it works as I said. – markets Feb 25 '14 at 21:29
  • I have now moved onto Answers as well and the strong_params have been added to the bottom of the page, I restarted the server and used the `proc` you suggested but it still doesn't work. – Tim Feb 25 '14 at 21:35
  • 1
    What version of Rails do you use? Very weird, it works for me pointing to 4.0.2. I also found this link http://stackoverflow.com/questions/20917161/no-implicit-conversion-of-type-array-to-integer-with-rails-4-0-2-and-strong-para, params seems well defined. – markets Feb 25 '14 at 21:44
  • Same as you Rails 4.0.2. Could you write out exactly how it should look then I will just copy and paste? – Tim Feb 25 '14 at 21:52
  • Never mind, got it working thanks for your help. The issue was an unrelated validation I had implemented without thinking it applied to this model. – Tim Feb 25 '14 at 22:02