Validation Rails3设计/omniauth-需要密码吗?和表格_:validate=>;真唐';我们不能一起工作

Validation Rails3设计/omniauth-需要密码吗?和表格_:validate=>;真唐';我们不能一起工作,validation,devise,omniauth,form-for,Validation,Devise,Omniauth,Form For,我正在使用Desive、omniauth和客户端验证,就像ryan bates railscasts中描述的那样 我现在面临着一个密码验证的问题,在通过omniauth第三方提供商注册时应该忽略这个问题 登记表(html.erb): 注册登记册 通过第三方网络资源名称:url=> 注册路径(资源名称):validate=>true)do | f |%> 直接注册按钮大%> 我的用户模型有一个-请参阅下面的更新 validates :password, :presence => true,

我正在使用Desive、omniauth和客户端验证,就像ryan bates railscasts中描述的那样

我现在面临着一个密码验证的问题,在通过omniauth第三方提供商注册时应该忽略这个问题

登记表(html.erb):

注册登记册 通过第三方网络资源名称:url=> 注册路径(资源名称):validate=>true)do | f |%> 直接注册

按钮大%>

我的用户模型有一个-请参阅下面的更新

validates :password, :presence => true, :confirmation =>true
子句和所需的密码?定义

def password_required?
(authentications.empty? || !password.blank?)
end
当我通过omniauth第三方提供商注册时,会正确弹出注册表,并要求用户输入电子邮件地址。不幸的是,用户必须输入密码,但由于通过第三方注册,因此不应提示用户

谁能给我一个提示,如何完成

谢谢并致以最良好的问候

一月

更新:为了提供更具体的视图,我添加了更多的代码片段

身份验证控制器: 用户模型: 类用户
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, 
:validatable, :email_regexp =>  /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

# Setup accessible (or protected) attributes for your model
attr_accessible :firstname, :lastname, :email, :password, :password_confirmation, :remember_me

# validations
validates :firstname, :presence => true
validates :lastname, :presence => true

validates :email, :presence => true, :uniqueness => true
validates_email :email

validates :password, :presence => true, :confirmation =>true            

# omniauth reference 3rd party
def apply_omniauth(omniauth)
if self.firstname.blank?
self.firstname = omniauth['info']['first_name'].presence || omniauth['info']['name'].presence || " "
end

if self.lastname.blank?
self.lastname = omniauth['info']['last_name'].presence || omniauth['info']['name'].presence || " "
end

if self.email.blank?
self.email = omniauth['info']['email'].presence
end

authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'])
end

# omit validation for omniauth session
def password_required?
(authentications.empty? || !password.blank?) && super
end

end
认证模型 类身份验证
# Setup accessible (or protected) attributes for your model  
attr_accessible :user_id, :provider, :uid
end
调试时,我发现'apply_omniauth(omniauth)'方法中的'authentications.build()'生成一个空对象,因此'password_required'始终为true,并且必须提供密码

补充问题: 为什么“authentications.empty”总是返回“false”

提前谢谢


Jan

首先,需要密码吗?方法的定义应如下所示:

def password_required?
  (authentications.empty? || !password.blank?) && super
end
# app/views/registrations/new.html.erb
<% if @user.password_required? %>
  <%= f.label :password %>
  <%= f.password_field :password %>
  <%= f.label :password_confirmation %>
  <%= f.password_field :password_confirmation %>
<% else %>
  ...
<% end %>
然后,在您看来,您应该像这样包装您的密码字段:

def password_required?
  (authentications.empty? || !password.blank?) && super
end
# app/views/registrations/new.html.erb
<% if @user.password_required? %>
  <%= f.label :password %>
  <%= f.password_field :password %>
  <%= f.label :password_confirmation %>
  <%= f.password_field :password_confirmation %>
<% else %>
  ...
<% end %>
#app/views/registrations/new.html.erb
...
然后,在您的控制器或模型中,当首次创建身份验证方法时,您应该使用build而不是create或new,因为这样
身份验证。empty?
将返回true,并要求您输入密码


最后,关于验证,您应该看看关于自定义验证的。您可能需要覆盖密码验证。

Hi Ashitaka,谢谢您的回复。您知道我在哪里忘记了用户模型中的&&super子句。是的,我正在尝试设计、omniauth和客户端验证(都是按照ryan bates的铁路计划实施的)。-我发现我的问题在于用户模型,在调用apply_omniauth(omniauth)时从第三方网络(如twitter)创建用户。authentications.build似乎不起作用。目前我正在调查为什么会发生这种情况。已解决!-我解决了为什么没有创建通过第三方注册的新用户的问题。在我的用户模型中,我对某些字段进行了验证:firstname、lastname、email、password->删除代码后,现在一切正常@Ashitaka你的验证是对的这似乎是我的问题!谢谢这个问题急需编辑。