simple_form&引导程序验证不起作用 [英] simple_form & bootstrap validations not working

查看:97
本文介绍了simple_form&引导程序验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要设置创建帐户"页面.我正在使用的宝石是:

I want to setup a 'Create Account' page. The gems I'm using are:

  • 导轨(3.2.3)
  • simple_form(2.0.1)
  • omniauth-identity
  • twitter-bootstrap-rails(2.0.6)
  • 蒙古人(2.2.3)

该表单如下所示:

= simple_form_for @identity, :url => '/auth/identity/register', :html => { :class =>    'form-horizontal' } do |f| 
  = f.input :name, :input_html => {:name => 'name'}
  = f.input :email, :input_html => {:name => 'email'}
  = f.input :password, :as => 'password', :input_html => {:name => 'password'} 
  = f.input :password_confirmation, :label => "Confirm Password", :as => 'password', :input_html => {:name => 'password_confirmation'} 
  .form-actions
    = f.button :submit, 'Sign Up', class: 'btn-primary'
    = link_to 'Cancel', root_path,  class: 'btn-danger'

对应的身份模型是

class Identity
  include Mongoid::Document
  include OmniAuth::Identity::Models::Mongoid
  field :name, :type => String
  field :email, :type => String
  field :password_digest, :type => String

  validates_presence_of :name, :email
  validates_uniqueness_of :email
  validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
end

如果我没有填写表格的任何文本字段,然后单击注册",则表格不会显示来自验证程序的任何错误消息,而是将我重定向到主页!

If I don't fill in any of the text field of the form and then click on 'Sign Up', the form does not display any error messages from the validators, but instead redirects me to the home page!

我是否缺少明显的东西,或者这可能是我使用的gem版本的问题?我当然可以使用twitter-bootstrap标记在没有simple_form的情况下实现相同的形式,但更喜欢这种方式.

Am I missing something obvious or could this be some problem with the gem versions I am using? I could of course implement the same form without simple_form using twitter-bootstrap markup, but would prefer it this way.

推荐答案

我也遇到类似的情况,其中simple_form会验证另一个模型中的字段,而不是用户模型中的字段.

I'm in a similar situation where simple_form does validate a field in another model, but not in the user model.

无论如何,我注意到您没有:

In any case, I noticed you don't have:

validates_presence_of :password

在您的模型中.也许mongoid可以使您免于执行该步骤?我不知道蒙古人,但对我而言,这就是验证该字段不为空的魔力.对于对我有用的领域,我有这个:

in your model. Maybe mongoid saves you from doing that step? I don't know mongoid, but for me that is the magic that validates that the field is not blank. For the field that is working for me I have this:

validates_presence_of :title, :localeLanguage, :message => "can't be blank"

这篇关于simple_form&引导程序验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆