我有一个名为PracticeQuestion的嵌套资源,它是PracticeQuiz的子代。我希望用户在练习问题时能够一次完成一个问题。例如:foo.com/practice_quizzes/1/practice_questions/1..n
我练习测验有效,但是当我尝试添加一个新的练习题时,我得到一个轨道错误,表示该参数丢失或为空,但我看不出我做错了什么。请帮忙
practice_quiz.rb
class PracticeQuiz < ApplicationRecord
belongs_to :user, optional: true
validates :user, presence: true
has_many :practice_questions, dependent: :destroy
end
practice_question.rb
class PracticeQuestion < ApplicationRecord
belongs_to :user, optional: true
belongs_to :practice_quiz
end
practice_questions_controller.rb
class PracticeQuestionsController < ApplicationController
before_action :set_practice_question, only: [:show, :edit, :update, :destroy]
def index
@practice_questions = PracticeQuestion.all
end
def show
end
# GET /practice_questions/new
def new
@practice_quiz = PracticeQuiz.friendly.find(params[:practice_quiz_id])
@practice_question = PracticeQuestion.new
end
# GET /practice_questions/1/edit
def edit
end
def create
@practice_quiz = PracticeQuiz.friendly.find(params[:practice_quiz_id])
@practice_question = PracticeQuestion.new(practice_question_params)
respond_to do |format|
if @practice_question.save
format.html { redirect_to @practice_question, notice: 'Practice question was successfully created.' }
format.json { render :show, status: :created, location: @practice_question }
else
format.html { render :new }
format.json { render json: @practice_question.errors, status: :unprocessable_entity }
end
end
end
private
def set_practice_question
@practice_question = PracticeQuestion.find(params[:id])
end
def practice_question_params
params.require(:practice_question).permit(:question, :explanation, :flagged)
end
end
视图/ practice_questions / _form.html.erb
<%= form_with(url: practice_quiz_practice_questions_path, local: true) do |form| %>
<% if practice_question.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(practice_question.errors.count, "error") %> prohibited this practice_question from being saved:</h2>
<ul>
<% practice_question.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= form.label :question %>
<%= form.text_field :question %>
</div>
<div class="field">
<%= form.label :explanation %>
<%= form.text_field :explanation %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
的routes.rb
resources :practice_quizzes do
resources :practice_questions
end
我设置了practice_questions控制器的新方法来查找父资源的id,但是我收到了这个错误。我很确定我也遵循rails命名约定。
ActionController :: ParameterMissing at / practice_quizzes / 23535 / practice_questions param丢失或值为空:practice_question
更新:这是rails服务器窗口的结果
Processing by PracticeQuestionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7dpxBB7jjZWzicCYWGA8yeTPSc9UeaqNDOavKQai2vMISryPBiMZ9Zo4LLS3DgZQI8IJc7rLh2TXd9Fj8PAjiA==", "question"=>"235235", "explanation"=>"25235232", "commit"=>"Save ", "practice_quiz_id"=>"23535"}
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
PracticeQuiz Load (0.9ms) SELECT "practice_quizzes".* FROM "practice_quizzes" WHERE "practice_quizzes"."slug" = $1 ORDER BY "practice_quizzes"."id" ASC LIMIT $2 [["slug", "23535"], ["LIMIT", 1]]
Completed 400 Bad Request in 102ms (ActiveRecord: 1.7ms)
ActionController::ParameterMissing - param is missing or the value is empty: practice_question:
app/controllers/practice_questions_controller.rb:76:in `practice_question_params'
app/controllers/practice_questions_controller.rb:30:in `create'
更新2:
视图/ practice_questions / new.html.erb
<h1>New Practice Question</h1>
<%= render 'form', practice_question: @practice_question %>
答案 0 :(得分:3)
这里
def practice_question_params
params.require(:practice_question).permit(:question, :explanation, :flagged)
end
您正在使用rails强参数。请参阅此回答https://stackoverflow.com/a/30826895/2627121
基本上,params.require(:practice_question)
表示您必须拥有practice_question
参数。
这里
Parameters: {"utf8"=>"✓", "authenticity_token"=>"", "question"=>"235235", "explanation"=>"25235232", "commit"=>"Save ", "practice_quiz_id"=>"23535"}
根据您的强参数声明,您必须具有question
和explanation
作为根参数
"practice_question" => { "question"=>"235235", "explanation"=>"25235232" }
您应该编辑表单字段,使其名称为practice_question[question]