gpt4 book ai didi

ruby-on-rails - 不允许的参数 : format

转载 作者:行者123 更新时间:2023-12-04 07:34:23 25 4
gpt4 key购买 nike

我不明白为什么我会收到这些 Unpermitted parameter: format消息,我正在执行 JSON 请求:POST "/questions/add_options.json"使用这些参数 Parameters: {"id_question"=>551, "options"=>[{"position"=>10, "label"=>"opc 10", "value"=>"opc 10", "go_page"=>nil}], "question"=>{}}这就是我在终端中得到的......

Started POST "/questions/add_options.json" for 127.0.0.1 at 2016-08-16 23:12:27 -0300
Processing by QuestionsController#add_options as JSON
Parameters: {"id_question"=>551, "options"=>[{"position"=>10, "label"=>"opc 10", "value"=>"opc 10", "go_page"=>nil}], "question"=>{}}
User Load (0.4ms) SELECT "login_aexa".* FROM "login_aexa" WHERE "login_aexa"."usuaex_id" = $1 ORDER BY "login_aexa"."usuaex_id" ASC LIMIT 1 [["usuaex_id", 1]]
Unpermitted parameter: format
Question Load (0.4ms) SELECT "questions".* FROM "questions" WHERE "questions"."id" = $1 LIMIT 1 [["id", 551]]
Unpermitted parameter: format
(0.2ms) BEGIN
(0.4ms) SELECT COUNT(*) FROM "options" WHERE "options"."question_id" = $1 [["question_id", 551]]

在 Rails Controller 中,我使用 params permit 拒绝不允许的参数,如下所示:
def question_add_options_params
params.permit(:id_question, options: [:position, :label, :value, :go_page], question: {})
end

在我看来格式应该没问题,任何人都知道我为什么要得到这些 Unpermitted parameter: format消息?

编辑 :

这是 Controller 的代码
class QuestionsController < ApplicationController
before_action :set_question, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!

# GET /questions
# GET /questions.json
def index
@questions = Question.all
end

# GET /questions/1
# GET /questions/1.json
def show
end

# GET /questions/new
def new
@question = Question.new
end

# GET /questions/1/edit
def edit
end

# POST /questions
# POST /questions.json
def create
@question = Question.new(question_params)

respond_to do |format|
if @question.save
format.html { redirect_to @question, notice: 'Question was successfully created.' }
format.json { render :show, status: :created, location: @question }
else
format.html { render :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 do |format|
if @question.update(question_params)
format.html { redirect_to @question, notice: 'Question was successfully updated.' }
format.json { render :show, status: :ok, location: @question }
else
format.html { render :edit }
format.json { render json: @question.errors, status: :unprocessable_entity }
end
end
end

def add_options
@question = Question.find(question_add_options_params[:id_question])

question_add_options_params[:options].each do|q_aop|
@question.options.create(q_aop)
end

@options = @question.options
end

# DELETE /questions/1
# DELETE /questions/1.json
def destroy
@question.destroy
respond_to do |format|
format.html { redirect_to questions_url, notice: 'Question was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_question
@question = Question.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def question_params
params[:question]
end

def question_add_options_params
params.permit(:id_question, options: [:position, :label, :value, :go_page])
end
end

最佳答案

params.permit(:id_question, options: [:position, :label, :value, :go_page], question: {})

这一行告诉 Rails 允许的唯一参数在上面的列表中。
如果您实际查看一个真正的 params-hash,它不仅包含表单传入的参数,还包含以下内容: :controller => :questions, :action => :create, :format => :json等等... Rails 总是根据 URL 插入

通常我们使用例如 form_for @question 来命名表单这意味着参数是这样输入的:
{:controller => :questions, :action => :create,
:format => :json,
:question => {"id_question"=>551, "options"=>[{"position"=>10, "label"=>"opc 10", "value"=>"opc 10", "go_page"=>nil}]}
}

那么你可以在你的 Controller 中做到这一点:
params.require(:question).permit(:id_question, options: [:position, :label, :value, :go_page])

这并没有从字面上告诉 rails 你不允许拥有总是由 rails 传入的 Controller / Action /格式参数......

显然,您需要修改它们的名称以满足您的需要,但这是您需要做的来阻止错误。

关于ruby-on-rails - 不允许的参数 : format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38987142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com