【问题标题】:Showing fields with errors for nested forms in Rails 3.2 + SimpleForm在 Rails 3.2 + SimpleForm 中显示嵌套表单的错误字段
【发布时间】:2012-04-02 14:43:46
【问题描述】:

我有一个 Flight 模型嵌套在 FlightLog 模型中。 FlightLog 可能包含许多航班。

我将 SimpleForm 与引导安装一起使用,这使得当验证失败时可以使用错误类将带有错误的表单元素包围起来。

问题是,即使为嵌套模型触发了验证,simple_fields_for 中存在错误的字段也没有被标记,因此无法确定哪个属性无效。

在调用 create 操作时检查错误哈希后,我可以看到它正确地填充了顶级错误,以及每个资源内嵌套资源的错误。

如何修改 simple_form 的行为以将错误类添加到每个嵌套模型的控制组以匹配父模型的行为?

提前致谢。

【问题讨论】:

    标签: ruby-on-rails ruby validation twitter-bootstrap simple-form


    【解决方案1】:

    如果您将 simple_form 与 bootstrap 一起使用,这确实有效 - 您只需正确设置一些项目:

    1 - 使用 simple_form 引导包装器(来自 simple_form 2.0) - 您可以在config/initializers/simple_form.rb (https://github.com/rafaelfranca/simple_form-bootstrap) 下的 github 存储库中找到它们

    2 - 要让嵌套表单显示错误,您必须确保提供了一个对象。 f.simple_fields_for :nested_model 不行,你需要使用f.simple_fields_for parent_model.nested_model 或者f.simple_fields_for :nested_model, parent_model.nested_model 这样表单才能获取到需要的对象。

    如果您仍然没有得到任何东西,请通过在嵌套对象上输出错误数据来验证表单是否确实获得了您认为的对象,但有错误:parent_model.nested_model.errors.full_messages.to_sentence

    【讨论】:

    • 感谢您的建议,我会试一试,告诉您进展如何!
    • parent_model.nested_model.errors.full_messages.to_sentence 对我不起作用,但 parent_model.errors.full_messages.to_sentence 有效,并显示来自嵌套模型的错误。谢谢!
    • 这是一个巨大的时间救星,我感激不尽
    【解决方案2】:

    我一直在使用自定义访问器而不是 _id 字段,这就是为什么他们在出现错误时没有得到通知的原因。我终于决定在每个访问器下使用 f.error :attr_name 并使用 JS 手动更改样式

    【讨论】:

    • 您可以添加更多代码示例吗?
    【解决方案3】:

    在此过程中可能会出现一些问题。我也有使用引导简单形式的问题。在我修复了控制器、模型和表单中的所有内容后,它就可以工作了。

    对我来说,我遇到了几个问题,尤其是对我来说至关重要的注释行。

    检查您是否具备以下条件:

    survey.rb:

    class Survey < ApplicationRecord
      has_many :answers
      accepts_nested_attributes_for :answers, allow_destroy: true
      #errors have to come from answer validation for answer form
      validates_associated :answers
      validates :question, :answers, presence: true
    

    answer.rb:

    class Answer < ApplicationRecord
      belongs_to :survey
      # make sure there is a validation on answer
      validates :answer, presence: true
    end
    

    _form.html.slim

    # make sure you have given the right attributes for .input and simple_fields_for
    = f.simple_fields_for :answers, @survey.answers do |answer_form|
      = answer_form.input :answer
    

    surveys_controller.rb

    def new
     @survey = Survey.new
     #when answers are not builded it wont show any simple fields for
     @survey.answers.build
    end
    
    def create
     @survey = Survey.new(survey_params)
     @survey.user = current_backend_user
     if @survey.save
      redirect_to backend_surveys_path, notice: 'Umfrage erfolgreich erstellt'
     else
      render :new
     end
    end
    
    def survey_params
      # make sure everything is permitted correctly
      params.require(:survey).permit(:some_attribute, ..., answers_attributes: %i[id answer])
    end
    

    reject if on model validation 有时也会导致错误。小心点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-14
      • 1970-01-01
      相关资源
      最近更新 更多