Ruby on rails 3 如何允许用户在designe/omniauth中删除授权时输入密码

Ruby on rails 3 如何允许用户在designe/omniauth中删除授权时输入密码,ruby-on-rails-3,devise,omniauth,Ruby On Rails 3,Devise,Omniauth,我有一个rais 3应用程序,它使用Desive和omniauth允许用户通过其twitter帐户和/或本地登录凭据注册/登录。注册和登录时一切正常。我的问题发生在用户选择在不首先建立本地密码的情况下销毁其twitter授权时。如果一个用户破坏了他们的授权,那么我想将他们路由到新的密码路径,这样他们就可以为将来的登录选择密码 以下是控制器代码: class AuthenticationsController < ApplicationController before_f

我有一个rais 3应用程序,它使用Desive和omniauth允许用户通过其twitter帐户和/或本地登录凭据注册/登录。注册和登录时一切正常。我的问题发生在用户选择在不首先建立本地密码的情况下销毁其twitter授权时。如果一个用户破坏了他们的授权,那么我想将他们路由到新的密码路径,这样他们就可以为将来的登录选择密码

以下是控制器代码:

   class AuthenticationsController < ApplicationController
      before_filter :authenticate_user!, :except => [:create, :failure]

      def create
        omniauth = request.env["omniauth.auth"]
        authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
        if authentication #existing user is logging-in with existing authentication service
          flash[:notice] = "Signed in successfully."
          set_home_location_cookies(authentication.user, authentication.user.home_lat, authentication.user.home_lng)
          sign_in(:user, authentication.user)
          redirect_to root_path
        elsif current_user #existing user who is already logged-in is creating a new authentication service for future use
          current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'], :token => omniauth['credentials']['token'])
          current_user.update_posting_preferences(omniauth['provider'])
          flash[:notice] = "Successfully linked to your #{omniauth['provider'].titleize} account."
          redirect_to root_path
        else #new user is creating a new authentication service and logging in
          user = User.new
          user.apply_omniauth(omniauth)
          if user.save
            flash[:notice] = "Signed in successfully."
            sign_in(:user, user)
            redirect_to root_path
          else
            session[:omniauth] = omniauth.except('extra')
            session[:user_message] = {:success => false, :message => "userSaveError"}
            redirect_to new_user_registration_url
         end
        end
      end


      def failure
        flash[:alert] = "Could not authorize you from your social service."
        redirect_to root_path
      end

      def destroy
        @authentication = current_user.authentications.find(params[:id])
        current_user.update_posting_preferences(@authentication.provider)
        @authentication.destroy
        flash[:notice] = "You have successfully destroyed your link to your #{@authentication.provider.titleize} account."
        if current_user.authentications.empty? && current_user.encrypted_password.empty?
          sign_out
          flash[:alert] = "Alert: Your account does not currently have a password for account authorization.  You are in danger of losing your account unless you create a new password by using this form."
          redirect_to new_password_path(current_user) and return
        else
          redirect_back_or(root_path)
        end
      end
类身份验证控制器[:创建,:失败]
def创建
omniauth=request.env[“omniauth.auth”]
身份验证=身份验证。通过提供程序和uid查找(omniauth['provider'],omniauth['uid'])
如果身份验证#现有用户正在使用现有身份验证服务登录
flash[:notice]=“已成功登录。”
设置家庭位置cookies(authentication.user、authentication.user.home\u lat、authentication.user.home\u lng)
登录(:user,authentication.user)
将\重定向到根\路径
elsif current_user(当前用户)#已登录的现有用户正在创建新的身份验证服务以供将来使用
当前_user.authentications.create!(:provider=>omniauth['provider'],:uid=>omniauth['uid'],:token=>omniauth['credentials']['token'])
当前用户。更新发布首选项(omniauth['provider'])
flash[:notice]=“已成功链接到您的#{omniauth['provider'].titleize}帐户。”
将\重定向到根\路径
否则#新用户正在创建新的身份验证服务并登录
user=user.new
user.apply\u omniauth(omniauth)
如果user.save
flash[:notice]=“已成功登录。”
登录(:用户,用户)
将\重定向到根\路径
其他的
会话[:omniauth]=omniauth.except('extra')
会话[:user_message]={:success=>false,:message=>userSaveError}
将\重定向到新\用户\注册\ url
结束
结束
结束
def故障
flash[:alert]=“无法从您的社交服务中授权您。”
将\重定向到根\路径
结束
def销毁
@authentication=当前用户.authentications.find(参数[:id])
当前用户。更新发布首选项(@authentication.provider)
@身份验证。销毁
flash[:notice]=“您已成功销毁指向{@authentication.provider.titleize}帐户的链接。”
如果当前_user.authentications.empty?&&当前用户。加密密码。是否为空?
注销
flash[:alert]=“alert:您的帐户当前没有帐户授权的密码。除非您使用此表单创建新密码,否则您将有丢失帐户的危险。”
重定向到新密码路径(当前用户)并返回
其他的
重定向\u返回\u或(根路径)
结束
结束
代码导致“找不到无效的映射”错误,该错误由my
将\u重定向到新的\u密码\u路径(当前用户)并返回
命令触发

我非常感谢你能帮我解决这个问题


谢谢

好的。我承认。我在一个教程中实现了身份验证控制器,而没有学习Desive routing来了解幕后的情况。昨晚我看了一下文件,解决了我的问题。有趣的是,上面的例程在旧版本的Desive上确实有效,但在Desive 1.5.3上不起作用

在销毁操作中,我注销当前用户,然后尝试路由到新的密码路径,并将“当前用户”作为参数发送进来。毫不奇怪,“current_user”此时已被置空。因此,我得到了“找不到nil的有效映射”错误。以下是我的简单解决方法:

  def destroy
    @authentication = current_user.authentications.find(params[:id])
    user = current_user
    current_user.update_posting_preferences(@authentication.provider)
    @authentication.destroy
    flash[:notice] = "You have successfully destroyed your link to your #{@authentication.provider.titleize} account."
    if current_user.authentications.empty? && current_user.encrypted_password.empty?
      sign_out
      flash[:alert] = "Alert: Your account does not currently have a password for account authorization.  You are in danger of losing your account unless you create a new password by using this form."
      redirect_to new_password_path(user) and return
    else
      redirect_back_or(root_path)
    end
  end