simple_form 和引导程序验证不起作用

2024-03-03

我想设置一个“创建帐户”页面。我使用的宝石是:

  • 导轨 (3.2.3)
  • 简单形式(2.0.1)
  • 全方位身份验证
  • 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

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

我是否遗漏了一些明显的东西,或者这可能是我正在使用的 gem 版本的问题?我当然可以使用 twitter-bootstrap 标记实现没有 simple_form 的相同表单,但更喜欢这种方式。


我处于类似的情况, simple_form 确实验证另一个模型中的字段,但不在用户模型中。

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

validates_presence_of :password

在你的模型中。也许 mongoid 可以让你免于执行这一步?我不知道 mongoid,但对我来说,这就是验证该字段不为空的魔力。对于对我有用的领域,我有这个:

validates_presence_of :title, :localeLanguage, :message => "can't be blank"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

simple_form 和引导程序验证不起作用 的相关文章

随机推荐