我想设置“创建帐户”页面。我使用的 gem 是:
- rails (3.2.3)
- simple_form (2.0.1)
- 全能身份
- twitter-bootstrap-rails (2.0.6)
- mongoid(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'
对应的Identity模型是
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"
关于ruby-on-rails - simple_form 和 Bootstrap 验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10374749/