Rails 4使用简单表单进行客户端验证

时间:2016-01-12 19:14:59

标签: ruby-on-rails ruby ruby-on-rails-4 client-side-validation

我创建了一个应用,我需要远程提交表单。如果表单未通过验证,则需要显示错误。如果成功提交,则应清除for。我使用Reform gem来剥离模型中的验证逻辑。

我已根据安装说明安装了client_side_validations以及client_side_validations-simple_form宝石。但由于某种原因,验证错误没有显示,我不知道为什么......我已经尝试了我能想到的所有调试选项。

这是我的代码:

  1. foo_form.rb

    class FooForm < ApplicationForm
    
      property :bar, validates: {
      presence:     true
      },client_validations: { class: 'Foo' }
    
    end
    
  2. foos_controller.rb

    def create 
      @foo = FooForm.new( Foo.new )
      respond_to do |format|
        if @foo.validate(params[ :foo ])
          @foo.save
          format.html { redirect_to root_path }
          format.json { render :show, status: :created, location: @foo.model }
        else
          format.html { render :new }
          format.json { render json: @foo.errors, status: :unprocessable_entity }
        end
      end
    end
    
  3. form.haml

    = simple_form_for @foo, url: foos_path, as: :foo, remote: true, validate: true do |f|
    .input-field
      = f.input :bar
    
    .right-align.card-button
      = f.button :submit, class: "waves-light blue", data: { disable_with: "Loading..." }
    
  4. 有人可以帮助找出我失踪的东西吗?感谢

0 个答案:

没有答案