使用rails中的simple_form_for无法使用空字段验证

时间:2015-03-05 16:34:00

标签: ruby-on-rails simple-form

我正在使用 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

请帮帮我。

1 个答案:

答案 0 :(得分:0)

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