ruby on rails - param is missing or the value is empty: answer -


i'm new ruby on rails , i'm not understanding i'm doing wrong. have form submitting question multiple choice trivia game question supposed saved question database table , 4 answers saved answer table question_id foreign key. whenever click submit button valid data entered fields, keep getting error, "param missing or value empty: answer" block of code in questions controller:

def answer_params    params.require(:answer).permit({:question_id => [@question.id]},:answer_text, :correct) end 

here full class questions_controller.rb:

class questionscontroller < applicationcontroller  def new   @question = question.new   @answer1 = answer.new   @answer2 = answer.new   @answer3 = answer.new   @answer4 = answer.new end    def create     @question = question.new(question_params)     if @question.save       @answer1 = answer.new(answer_params)       @answer2 = answer.new(answer_params)       @answer3 = answer.new(answer_params)       @answer4 = answer.new(answer_params)       if @answer1.save && @answer2.save && @answer3.save && @answer4.save        redirect_to root_path     end     else       render 'new'     end   end    private    def question_params     params.require(:question).permit(:username, :question)   end    def answer_params     params.require(:answer).permit({:question_id => [@question.id]}, :answer_text, :correct)   end  end 

answers_controller.rb:

class answerscontroller < applicationcontroller      def new         @answer = answer.new(answer_params)      end      def create         @question = question.find(params[:id])         @answer = answer.new(answer_params)          if @answer.save             redirect_to root_path         end     end      private   def answer_params     params.require(:answer).permit({:question_id => [@question.id]}, :answer_text, :correct)   end end 

new.html.erb:

<%= form_for(@question) |f| %>      <%= f.hidden_field :username, :value => current_user.email %>     <br>     <%= f.label :category %>     <%= f.text_field :category %>     <br>     <%= f.label :question %>     <%= f.text_field :question %>     <br>     <%= f.fields_for(@answer1) |c| %>         <%= c.label :correct_answer %>         <%= c.text_field :answer_text %>         <%= c.hidden_field :correct, :value => true %>         <br>     <% end %>     <%= f.fields_for(@answer2) |i| %>         <%= i.label :incorrect_answer1 %>         <%= i.text_field :answer_text %>         <%= i.hidden_field :correct, :value => false %>         <br>     <% end %>     <%= f.fields_for(@answer3) |j| %>         <%= j.label :incorrect_answer2 %>         <%= j.text_field :answer_text %>         <%= j.hidden_field :correct, :value => false %>         <br>     <% end %>     <%= f.fields_for(@answer4) |k| %>         <%= k.label :incorrect_answer3 %>         <%= k.text_field :answer_text %>         <%= k.hidden_field :correct, :value => false %>     <% end %>     <br>     <%= f.submit "submit question", class: "btn btn-primary"%> <% end %> 

question.rb:

class question < activerecord::base     belongs_to :category     has_many :answers end 

answer.rb:

class answer < activerecord::base   belongs_to :question end 

can tell me i'm going wrong?


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -