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

2 Answers 2

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? }
Sign up to request clarification or add additional context in comments.

2 Comments

Removing that completely makes no difference, the error was still happening before that was added.
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).
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

7 Comments

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 can't be, as I already said the error still happens when that is removed.
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.
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.
What version of Rails do you use? Very weird, it works for me pointing to 4.0.2. I also found this link stackoverflow.com/questions/20917161/…, params seems well defined.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.