在 simple_form 中为 nil:NilClass 的 Rails 未定义方法“错误" [英] Rails undefined method `errors' for nil:NilClass in simple_form
问题描述
我正在使用 simple_form
并且我想在部分内部向用户显示错误.为此,我有一个非常简单的形式:
I'm using simple_form
and I want to display errors to the user inside of partial. To do so I've got pretty simple form:
# test_result/shared/_form.html.erb
<%= simple_form_for :test_results, url: url do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<% randomize_questions.each_with_index.map do |q, i| %>
<%= f.input "[#{i}][answer]", collection: q[:answers].map.with_index { |a, i| [a, i.to_s] }, as: :radio_buttons %>
<%= f.input "[#{i}][id]", as: :hidden, input_html: { value: q[:id] } %>
<% end %>
<%= f.button :submit, 'Submit' %>
<% end %>
它通过以下方式在 new.html.erb
内部呈现:
It's rendered inside of new.html.erb
via:
<%= render 'test_result/shared/form', url: test_step_one_path, randomize_questions: @randomize_questions %>
控制器也很标准:
class TestBaseController < SignupBaseController
before_action :randomize_questions, only: %i[new create]
def new
@test_result = TestResult.new
end
def create
@test_result = current_user.test_results.new(
answer: test_result_params,
)
if @test_result.save
redirect_to next_step_path
else
render :new, status: :unprocessable_entity
end
end
private
def test_result_params
params.require(:test_results).permit(question_answer_params)
end
def question_answer_params
(0..@randomize_questions.length).each_with_object({}) do |i, hash|
hash[i.to_s] = %i[id answer question]
end
end
这是注册过程中的步骤之一.但是我无法使用此表单显示页面,因为出现以下错误:
This is one of the steps in the registration process. But I cannot display the page with this form because the below error shows up:
undefined method errors for nil:NilClass
Extracted source (around line #3):
1 <%= simple_form_for :appropriateness_test_results, url: url do |f| %>
2 <%= f.error_notification %>
3 <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
这意味着 f.object = nil
- 我传递了什么错误吗?它应该是什么样子,以便我可以向用户显示错误?
Which means f.object = nil
- did I passed something wrong? How should it look like so that I can display errors to the user?
我在控制器内部添加了 test_params
.当用户没有选择任何内容时,question_answer_params
将产生以下哈希值:
I've added test_params
inside of controller. The question_answer_params
will produce below hash when the user does not select anything:
# question_answer_params output
{"0"=>{"id"=>"21", "answer"=>""},
"1"=>{"id"=>"22", "answer"=>""},
"2"=>{"id"=>"23", "answer"=>""},
"3"=>{"id"=>"18", "answer"=>""},
"4"=>{"id"=>"19", "answer"=>""},
"5"=>{"id"=>"20", "answer"=>""}}
推荐答案
我认为 render 应该是
I think render should be
<%= render 'test_result/shared/form', url: test_step_one_path, randomize_questions: @randomize_questions, test_results: @test_result %>
或
形式应该像
<%= simple_form_for @test_result, url: url do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<% randomize_questions.each_with_index.map do |q, i| %>
<%= f.input "[#{i}][answer]", collection: q[:answers].map.with_index { |a, i| [a, i.to_s] }, as: :radio_buttons %>
<% end %>
<%= f.button :submit, 'Submit' %>
<% end %>
这篇关于在 simple_form 中为 nil:NilClass 的 Rails 未定义方法“错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!