ruby on rails - Some records are not being saved in nested forms -
so im doing quiz app. quizzes controller:
# encoding: utf-8 class quizzescontroller < applicationcontroller before_action :set_quiz, only: [:show, :edit, :update, :destroy] before_action :authenticate_user! # /quizzes # /quizzes.json def index @quizzes = quiz.all end # /quizzes/1 # /quizzes/1.json def show end # /quizzes/new def new @quiz = quiz.new 10.times question = @quiz.questions.build 4.times { question.answers.build } end 5.times @quiz.links.build end end # /quizzes/1/edit def edit end # post /quizzes # post /quizzes.json def create @quiz = quiz.new(quiz_params) respond_to |format| if @quiz.save format.html { redirect_to @quiz, notice: 'quiz created.' } format.json { render action: 'show', status: :created, location: @quiz } else format.html { render action: 'new' } format.json { render json: @quiz.errors, status: :unprocessable_entity } end end end # patch/put /quizzes/1 # patch/put /quizzes/1.json def update respond_to |format| if @quiz.update(quiz_params) format.html { redirect_to @quiz, notice: 'quiz updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @quiz.errors, status: :unprocessable_entity } end end end # delete /quizzes/1 # delete /quizzes/1.json def destroy @quiz.destroy respond_to |format| format.html { redirect_to quizzes_url } format.json { head :no_content } end end private # utilize callbacks share mutual setup or constraints between actions. def set_quiz @quiz = quiz.find(params[:id]) end # never trust parameters scary internet, allow white list through. def quiz_params params.require(:quiz) .permit(:content, links_attributes: [:id, :name, :www], question_attributes: [:id, :content, answer_attributes: [:id, :content, :correct]]) end end
question controller:
# encoding: utf-8 class questionscontroller < applicationcontroller before_action :set_question, only: [:show, :edit, :update, :destroy] before_action :set_quizzes, only: [:new, :show, :edit, :update, :destroy] # /questions # /questions.json def index @questions = question.all end # /questions/1 # /questions/1.json def show end # /questions/new def new @question = question.new end # /questions/1/edit def edit end # post /questions # post /questions.json def create @question = question.new(question_params) respond_to |format| if @question.save format.html { redirect_to @question, notice: 'question created.' } format.json { render action: 'show', status: :created, location: @question } else format.html { render action: 'new' } format.json { render json: @question.errors, status: :unprocessable_entity } end end end # patch/put /questions/1 # patch/put /questions/1.json def update respond_to |format| if @question.update(question_params) format.html { redirect_to @question, notice: 'question updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @question.errors, status: :unprocessable_entity } end end end # delete /questions/1 # delete /questions/1.json def destroy @question.destroy respond_to |format| format.html { redirect_to questions_url } format.json { head :no_content } end end private # utilize callbacks share mutual setup or constraints between actions. def set_question @question = question.find(params[:id]) end def set_quizzes @quizzes = quiz.find(:all).map |quiz| [quiz.content, quiz.id] end end # never trust parameters scary internet, allow white list through. def question_params params.require(:question) .permit(:content, :quiz_id, answer_attributes: [:id, :content, :correct]) end end
and answer:
# encoding: utf-8 class answerscontroller < applicationcontroller before_action :set_answer, only: [:show, :edit, :update, :destroy] before_action :set_questions, only: [:new, :show, :edit, :update, :destroy] # /answers # /answers.json def index @answers = answer.all end # /answers/1 # /answers/1.json def show end # /answers/new def new @answer = answer.new end # /answers/1/edit def edit end # post /answers # post /answers.json def create @answer = answer.new(answer_params) respond_to |format| if @answer.save format.html { redirect_to @answer, notice: 'answer created.' } format.json { render action: 'show', status: :created, location: @answer } else format.html { render action: 'new' } format.json { render json: @answer.errors, status: :unprocessable_entity } end end end # patch/put /answers/1 # patch/put /answers/1.json def update respond_to |format| if @answer.update(answer_params) format.html { redirect_to @answer, notice: 'answer updated.' } format.json { head :no_content } else format.html { render action: 'edit' } format.json { render json: @answer.errors, status: :unprocessable_entity } end end end # delete /answers/1 # delete /answers/1.json def destroy @answer.destroy respond_to |format| format.html { redirect_to answers_url } format.json { head :no_content } end end private # utilize callbacks share mutual setup or constraints between actions. def set_answer @answer = answer.find(params[:id]) end def set_questions @questions = question.find(:all).map |question| [question.content, question.id] e nd end # never trust parameters scary internet, allow white list through. def answer_params params.require(:answer).permit(:content, :correct, :question_id) end end model: class quiz < activerecord::base has_and_belongs_to_many :users has_many :questions has_many :links accepts_nested_attributes_for :links accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true end class question < activerecord::base belongs_to :quiz has_many :answers accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true end
and form:
<%= form_for(@quiz) |f| %> <% if @quiz.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@quiz.errors.count, "error") %> prohibited quiz beingness saved:</h2> <ul> <% @quiz.errors.full_messages.each |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <p> <%= f.label :content, "nazwa quizu" %> <%= f.text_field :content %> </p> <ol> <%= f.fields_for :links |builder| %> <li><%= render "link_fields", :f => builder %></li> <% end %> </ol> <ol><%= f.fields_for :questions |builder| %> <li><%= render "question_fields", :f => builder %></li> <% end %> </ol> <p><%= f.submit "submit" %></p>
the form works links, when comes saving questions , answers nil (when listed records database, there questions saved using form , method). code below created display questions made other form , worked, since changed method bit, have alter code in view? or maybe it's controllers fault?
this code displaying:
<div class="row"> <div id="prezentacja" class="col-md-7 col-lg-7 col-sm-12 prezentacja3 center-block3"> <p class="header2 roboto"> <%= @quiz.content %> </p> <%= notice %> <div class="col-md-12 col-lg-12" > <% @quiz.questions.each |question| %> <div id="answer-<%= question.id %>" class="question col-md-12 col-lg-12"> <h4><%= question.content %></h4> <% question.answers.each |answer| %> <div class="col-lg-4 answer"> <p><%= answer.content %></p> </div> <% end %> </div> <% end %> </div> <%= answer.all.to_a %> </div> <div id="prezentacja" class=" col-md-3 col-lg-3 col-sm-12 prezentacja3 center-block3"> <div class="row"> <div class="col-md-12 col-lg-12 col-sm-12"> <% @quiz.links.each |link| %> <%= link_to link.name, "http://#{link.www}" %> <% end %> </div> </div> <div class="row"> <div class="col-md-12 col-lg-12 col-sm-12"> <p>dupa dupa dupa</p> </div> </div> </div> </div>
you have typo in quiz controller:
def quiz_params params.require(:quiz) .permit(:content, links_attributes: [:id, :name, :www], question_attributes: [:id, :content, answer_attributes: [:id, :content, :correct]]) end
it should questions_attributes
. 'question_params' there should 'answers_attributes'. why have dupa dupa dupa
in view? :p
ruby-on-rails ruby forms ruby-on-rails-4 nested-forms
No comments:
Post a Comment