首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未知属性“”answered_questions_attributes“”

未知属性“”answered_questions_attributes“”
EN

Stack Overflow用户
提问于 2018-08-07 20:37:56
回答 1查看 379关注 0票数 0

我正在尝试创建一个表单,用户可以在其中回答问题并提交到结果页面,然而,当我试图通过测验控制器传递AnsweredQuestions属性时,它告诉我这是一个“未知属性”。

接收错误: AnsweredQuestion的属性'answered_questions_attributes‘未知。

Quiz.rb:

代码语言:javascript
复制
 class Quiz < ApplicationRecord
  validates :title, presence: true, length: { maximum: 50 }
  has_many :questions, dependent: :destroy
  has_many :answered_questions, through: :questions, dependent: :destroy
  accepts_nested_attributes_for :answered_questions, reject_if: :all_blank, allow_destroy: true
  accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true
end

AnsweredQuestion.rb:

代码语言:javascript
复制
class AnsweredQuestion < ApplicationRecord
  belongs_to :user
  belongs_to :question
  belongs_to :answer
  belongs_to :quiz
end

测验控制器:

代码语言:javascript
复制
  def show
    @quiz = Quiz.find(params[:id])
    @questions = Question.all
    @answered_questions = current_user.answered_questions.build

    @quiz.answered_questions.build

  end

  def create 
    @quiz = Quiz.new(show_params)
    if @quiz.save
      flash[:success] = "You have created a new quiz!"
      redirect_to @quiz
    else 
      render 'new'
    end
  end


  def post_answered_questions 
    @answered_question = current_user.answered_questions.build(show_params)
    if @answered_question.save
      flash[:success] = "You have completed the quiz!"
      redirect_to results_quiz_path(params[:quiz][:id])
    else
      render ''
    end
  end

  private

  def user_completed_quiz 
    if(current_user.answered_questions.pluck(:quiz_id).uniq.include?(params[:id].to_i)) 
      redirect_to quizzes_path
    end
  end

  def show_params
    params.require(:quiz).permit(:title, answered_questions_attributes: [:id, :answer_id, :question_id, :user_id, :quiz_id], questions_attributes: [:id, :question_title, :quiz_id, :done, :_destroy, answers_attributes: [:id, :answer_title, :question_id, :quiz_id, :correct_answer, :_destroy]])
  end
end

show.html.erb (测验中):

代码语言:javascript
复制
<%= form_for(@quiz, url: post_answered_questions_quizzes_path, method: "POST") do |f| %>

<%= @quiz.title %>

<%= f.hidden_field :id, :value => @quiz.id %>

<% @quiz.questions.each do |question| %>

<%= f.fields_for :answered_questions do |answer_ques| %>

    <h4><%= question.question_title %></h4>

        <%= answer_ques.hidden_field :question_id, :value => question.id %>
        <%= answer_ques.hidden_field :quiz_id, :value => @quiz.id %>
        <%= answer_ques.select(:answer_id, options_for_select(question.answers.map{|q| [q.answer_title, q.id]})) %>

    <% end %>


    <% end %>

    <%= submit_tag %>

    <% end %> 

更新:

show.html.erb (测验):

代码语言:javascript
复制
<%= form_for(@answered_questions, url: answered_questions_path, method: "POST") do |f| %>

<%= @quiz.title %>

<%= f.hidden_field :id, :value => @quiz.id %>

<% @quiz.questions.each do |question| %>

<%= f.fields_for :answered_questions do |answer_ques| %>

    <h4><%= question.question_title %></h4>

        <%= answer_ques.hidden_field :question_id, :value => question.id %>
        <%= answer_ques.hidden_field :quiz_id, :value => @quiz.id %>
        <%= answer_ques.select(:answer_id, options_for_select(question.answers.map{|q| [q.answer_title, q.id]})) %>

    <% end %>


    <% end %>

    <%= submit_tag %>

    <% end %> 

AnsweredQuestion控制器:

代码语言:javascript
复制
class AnsweredQuestionsController < ApplicationController


  def show
    @answered_question = AnsweredQuestion.new
  end

  def create
    @answered_question = current_user.answered_questions.build(answered_params)
    binding.pry
    if @answered_question.save
      flash[:success] = "You have completed the quiz!"
      redirect_to results_quiz_path(params[:quiz][:id])
    else
      render ''
    end
  end

  def edit
    @answered_questions = AnsweredQuestion.find(params[:id])
  end


  def destroy
    AnsweredQuestion.find(params[:id]).destroy
    flash[:success] = "Answered quiz deleted"
    redirect_to answered_questions_url
  end

  private


  def answered_params
    params.require(:answered_questions).permit(:question_id, :answer_ids, :user_id, :quiz_id, :id, :_destroy)
  end

end
EN

回答 1

Stack Overflow用户

发布于 2018-08-07 21:04:22

看起来您正在接受Quiz的嵌套属性,但是在AnsweredQuestion中使用嵌套属性,而不是在Quiz中。您需要在另一个模型中添加accepts_nested_attributes_for,并且在show_params中它应该是quiz_attributes而不是answered_questions_attributes。尽管您正在使用show_params两次,一次在一个模型上,第二个在另一个模型上,因此您可能需要两个参数。否则你会弄坏另一个。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51726998

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档