0

我是 Ruby on Rails 的新手,我想建立一个带有 facebook 和 twitter 的omniauth-devise 系统,作为我应用程序的一种登录方式。现在我几乎(我认为)即将让它运行起来。但是,我收到以下错误:

路由错误

未初始化的常量 AuthServicesController

尝试运行 rake 路线以获取有关可用路线的更多信息。

而且我无法继续登录。

我一直在网上做很多研究,但我找不到任何有相同或相似问题的人。

有人知道我是什么吗?我马上就完全迷路了。

非常感谢!

顺便说一句,我在 Services_controller.rb 中得到的所有代码如下:

此链接以漂亮的术语将您带到源代码http://www.sourcepod.com/ubfirz26-7741

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

def index
  # get all authentication services assigned to the current user
  @services = current_user.services.all
end

def destroy
  # remove an authentication service linked to the current user
  @service = current_user.services.find(params[:id])
  @service.destroy

  redirect_to services_path
end

def create
  # get the service parameter from the Rails router
  params[:service] ? service_route = params[:service] : service_route = 'no service (invalid callback)'

  # get the full hash from omniauth
  omniauth = request.env['omniauth.auth']

  # continue only if hash and parameter exist
  if omniauth and params[:service]

    # map the returned hashes to our variables first - the hashes differ for every service
    # omniauth['extra']['raw_info'] o omniauth['extra']['raw_info'] . Aquest últim és el més modern
    if service_route == 'facebook'
      omniauth['extra']['raw_info']['email'] ? email =  omniauth['extra']['raw_info']['email'] : email = ''
      omniauth['extra']['raw_info']['name'] ? name =  omniauth['extra']['raw_info']['name'] : name = ''
      omniauth['extra']['raw_info']['id'] ?  uid =  omniauth['extra']['raw_info']['id'] : uid = ''
      omniauth['provider'] ? provider =  omniauth['provider'] : provider = ''
    elsif service_route == 'github'
      omniauth['user_info']['email'] ? email =  omniauth['user_info']['email'] : email = ''
      omniauth['user_info']['name'] ? name =  omniauth['user_info']['name'] : name = ''
      omniauth['extra']['raw_info']['id'] ?  uid =  omniauth['extra']['raw_info']['id'] : uid = ''
      omniauth['provider'] ? provider =  omniauth['provider'] : provider = ''
    elsif service_route == 'twitter'
      email = ''    # Twitter API never returns the email address
      #omniauth['user_info']['name'] ? name =  omniauth['user_info']['name'] : name = ''
      omniauth['info']['name'] ? name =  omniauth['info']['name'] : name = ''
      omniauth['uid'] ?  uid =  omniauth['uid'] : uid = ''
      omniauth['provider'] ? provider =  omniauth['provider'] : provider = ''
    #elsif service_route == 'google'
    #   omniauth['user_info']['email'] ? email =  omniauth['user_info']['email'] : email = ''
   #    omniauth['user_info']['name'] ? name =  omniauth['user_info']['name'] : name = ''
    #   omniauth['uid'] ? uid =  omniauth['uid'] : uid = ''
   #    omniauth['provider'] ? provider =  omniauth['provider'] : provider = ''
    else

      # we have an unrecognized service, just output the hash that has been returned
      render :text => omniauth.to_yaml
      #render :text => uid.to_s + " - " + name + " - " + email + " - " + provider
      return
    end

    # continue only if provider and uid exist
    if uid != '' and provider != ''
      # nobody can sign in twice, nobody can sign up while being signed in (this saves a lot of trouble)
      if !user_signed_in?
        # check if user has already signed in using this service provider and continue with sign in process if yes
        auth = Service.find_by_provider_and_uid(provider, uid)
        if auth
          flash[:notice] = 'Signed in successfully via ' + provider.capitalize + '.'
          sign_in_and_redirect(:user, auth.user)
        else
          # check if this user is already registered with this email address; get out if no email has been provided
          if email != ''
            # search for a user with this email address
            existinguser = User.find_by_email(email)
            if existinguser
              # map this new login method via a service provider to an existing account if the email address is the same
              existinguser.services.create(:provider => provider, :uid => uid, :uname => name, :uemail => email)
              flash[:notice] = 'Sign in via ' + provider.capitalize + ' has been added to your account ' + existinguser.email + '. Signed in successfully!'
              sign_in_and_redirect(:user, existinguser)
            else
              # let's create a new user: register this user and add this authentication method for this user
              name = name[0, 39] if name.length > 39             # otherwise our user validation will hit us

              # new user, set email, a random password and take the name from the authentication service
              #user = User.new :email => email, :password => SecureRandom.hex(10), :fullname => name
              user = User.new :email => email, :password => SecureRandom.hex(10), :name => name, :haslocalpw => false
              # add this authentication service to our new user
             # user.services.build(:provider => provider, :uid => uid, :uname => name, :uemail => email)
              user.services.build(:provider => provider, :uid => uid, :uname => name, :uemail => email)

              # do not send confirmation email, we directly save and confirm the new record
              user.skip_confirmation!
              user.save!
              user.confirm!

              # flash and sign in
              flash[:myinfo] = 'Your account on CommunityGuides has been created via ' + provider.capitalize + '. In your profile you can change your personal information and add a local password.'
              sign_in_and_redirect(:user, user)
            end
          else
            flash[:error] =  service_route.capitalize + ' can not be used to sign-up on CommunityGuides as no valid email address has been provided. Please use another authentication provider or use local sign-up. If you already have an account, please sign-in and add ' + service_route.capitalize + ' from your profile.'
            redirect_to new_user_session_path
          end
        end
      else
        # the user is currently signed in

        # check if this service is already linked to his/her account, if not, add it
        auth = Service.find_by_provider_and_uid(provider, uid)
        if !auth
          current_user.services.create(:provider => provider, :uid => uid, :uname => name, :uemail => email)
          flash[:notice] = 'Sign in via ' + provider.capitalize + ' has been added to your account.'
          redirect_to services_path
        else
          flash[:notice] = service_route.capitalize + ' is already linked to your account.'
          redirect_to services_path
        end  
      end  
    else
      flash[:error] =  service_route.capitalize + ' returned invalid data for the user id.'
      redirect_to new_user_session_path
    end
  else
    flash[:error] = 'Error while authenticating via ' + service_route.capitalize + '.'
    redirect_to new_user_session_path
  end
end
end
4

1 回答 1

1

Your class is declared as:

class ServicesController < ApplicationController

And you are getting this error:

uninitialized constant AuthServicesController

Look at the difference between ServicesController and AuthServicesController and I believe you have find the solution to your problem. In other words, I believe you should declare your class as:

class AuthServicesController < ApplicationController
于 2012-06-22T15:27:33.480 回答