【问题标题】:Empty field validation not working using simple_form_for in rails空字段验证在 rails 中使用 simple_form_for 不起作用
【发布时间】:2015-03-06 00:39:08
【问题描述】:

我正在为我的表单使用 gem 'simple_form'。表单工作正常,但是当我尝试实现空字段验证(通过在输入标签中传递 validate: true )时,它不起作用。 如果我通过 input_html: { maxlength: 20 } 等;它工作正常。

_form.html.erb:

<%= simple_form_for([@employee, @insurance]) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :name_of_dependent, required: true %>
    <br/>
    <%= f.input :relationship %>
   </div>
<br/>
  <div class="form-actions">
    <%= f.button :submit, class: 'btn btn-success btn-md' %>
  </div>
<% end %>

simple_form.en.yml:

en:
  simple_form:
    "yes": 'Yes'
    "no": 'No'
    required:
      text: 'required'
      mark: '*'
      # You can uncomment the line below if you need to overwrite the whole required html.
      # When using html, text and mark won't be used.
      # html: '<abbr title="required">*</abbr>'
    error_notification:
      default_message: "Please review the problems below:"
    # Labels and hints examples
    # labels:
    #   defaults:
    #     password: 'Password'
    #   user:
    #     new:
    #       email: 'E-mail to sign in.'
    #     edit:
    #       email: 'E-mail.'
    # hints:
    #   defaults:
    #     username: 'User name to sign in.'
    #     password: 'No special characters, please.'

我的控制器代码是:

  def new
      @employee = Employee.find(params[:employee_id])
     @insurance = @employee.insurances.build
  end

  def create
    @employee = Employee.find(params[:employee_id])
    @insurance = @employee.insurances.create(insurance_params)
    redirect_to employee_path(@employee)  
  end

   def insurance_params
      params.require(:insurance).permit(:name_of_dependent, :relationship)
    end

请帮帮我。

【问题讨论】:

    标签: ruby-on-rails simple-form


    【解决方案1】:

    未经测试:我遇到了同样的问题并安装了Nilify_Blanks

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-24
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多