隐藏&在视图中显示密码字段,使用Omniauth和facebook登录

时间:2013-12-05 20:49:56

标签: ruby-on-rails facebook ruby-on-rails-3 devise passwords

晚上好!

我遇到了一些问题,我会在一些情况下解释一下!

涉及的宝石: Devise,Omniauth,Omniauth-facebook

我和设计一起设置了omniauth,允许我的网站用户通过Facebook登录/注册,它完美无缺。 除此之外,我还有一个常规注册页面,用户可以在其中输入用户名,电子邮件和密码以注册到网站。

当用户希望在注册后编辑信息时,问题就出现了 - 通过“编辑”视图。我想隐藏Facebook注册用户的密码字段(因为他们当然没有密码),只留下姓名和电子邮件(现在),但是为常规注册用户显示密码字段,以便他们可以更改密码。

与此同时,我设法绕过设计强制的“输入确认密码”,因此Facebook用户无需输入常规用户所做的确认密码(因为不存在)。 通过user.rb中的以下内容实现:

def update_with_password(params, *options)
 if encrypted_password.blank?
 update_attributes(params, *options)
else
 super
 end
end

要解决我的密码问题,我尝试将以下内容添加到我的user.rb:

   def password_required?
    (authentications.empty? || !password.blank?) && super
   end

在我的设计\ registrations \ edit.html.erb中跟随以下内容:

<% if @user.password_required? %>
  <%= f.input :current_password, :hint => "We need your current password to confirm any changes", :required => false %>
 <h5>Change your password?</h5>
  <%= f.input :password, :autocomplete => "off", :hint => "Enter a new password above,      leave Password blank if you do not wish to change it", :required => false %>
  <%= f.input :password_confirmation, :required => false %>
<% end %>

遵循本指南的要点:http://www.orhancanceylan.com/rails-twitter-and-facebook-authentications-with-omniauth-and-devise/

密码输入被上面的代码隐藏,但是对于Facebook注册用户和普通注册用户都是隐藏的,所以我担心我不知所措!

请告诉我是否还有其他任何我应该发布的内容以帮助缩小问题范围,如果我错过任何相关内容,请向我道歉。

提前谢谢你:)

1 个答案:

答案 0 :(得分:0)

password_required?旨在检查是否验证状态&amp;仅在保存时确认密码。这是方法的定义,在lib / devise / models / validatable:50-55

  # Checks whether a password is needed or not. For validations only.
  # Passwords are always required if it's a new record, or if the password
  # or confirmation are being set somewhere.
  def password_required?
    !persisted? || !password.nil? || !password_confirmation.nil?
  end

请注意“仅限验证”。评价。

您应该使用不同的检查来确定用户是oauth用户还是电子邮件/密码用户。例如:

app / models / user.rb中的

def oauth_user?
  authentications.present?
end

然后直接检查视图中是否为@user.oauth_user?