2013-04-10 45 views
0

我使用omniauth寶石,允許用戶立即登記/登錄到我的Rails應用程序引發ArgumentError,錯誤的參數數目(3 1)在創建操作

這裏是我的Gemfile的一部分

gem 'omniauth' 
gem 'omniauth-twitter' 
gem 'twitter' 

Omniauth =是V1.1.4, Omniauth-twtter是v0.0.16, Omniauth-OAuth是1.0.1版, Twitter是4.6.2

我碰到這個線程想出了一個類似的錯誤wrong number of arguments (3 for 1) after upgrading rails from 3.1.1 to 3.1.3所以我嘗試卸載l Omniauth並降級到版本「〜> 0.2.6」,但是我得到了以下消息:

該捆綁包目前已將omniauth鎖定在1.1.4。 omniauth版本是問題或其他問題的原因嗎?

,我試圖修復該錯誤消息是這樣的,當我嘗試使用Twitter簽到

ArgumentError in AuthenticationsController#create 
wrong number of arguments (3 for 1) 

app/helpers/sessions_helper.rb:4:in `sign_in' 
app/controllers/authentications_controller.rb:12:in `create' 

這裏是我的authentications_controller.rb

class AuthenticationsController < InheritedResources::Base 

def index 
    @authentications = current_user.authentications if current_user 
end 

def create 
    omniauth = request.env['omniauth.auth'] 
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid']) 
    if authentication 
    flash[:notice] = "Signed in successfully" 
    sign_in_and_redirect(:user, authentication.user) 
    elsif current_user 
    token = omniauth['credentials'].token 
    secret = omniauth['credentials'].token 
    current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'], :token => token, :secret => token_secret) 
    flash[:notice] = "Authentication successful" 
    redirect_to authentications_url 
    else 
    user = User.new 
    user.apply_omniauth(omniauth) 
    if user.save! 
    flash[:notice] = "Signed in successfully" 
    sign_in(:user, authentication.user) 
    else 
    session[:omniauth] = omniauth.except('extra') 
    redirect_to '/signup' 
    end 
end 
end 

def destroy 
    @authentication = current_user.authentications.find(params[:id]) 
    @authentication.destroy 
    flash[:notice] = "Successfully destroyed authentication" 
    redirect_to authentications_url 

end 
end 

這裏是我的sessions_helper.rb

module SessionsHelper 

    def sign_in(user) 
    cookies.permanent[:remember_token] = user.remember_token 
    current_user = user 
    end 

    def sign_in_and_redirect(user) 
    redirect_to root_path 
    end 

這裏是我的sessions_controller.rb

class SessionsController < ApplicationController 
    def create 
    user = User.find_by_email(params[:session][:email]) 
    if user && user.authenticate(params[:session][:password]) 
     sign_in user 
     redirect_to root_url 
    else 
     flash.now[:error] = "Invalid email/password combination" 
     render 'new' 
    end 
    end 

這裏的user.rb模型

class User < ActiveRecord::Base 
    attr_accessible :name, :email, :password, :password_confirmation 
    has_secure_password 
    has_many :authentications 

    before_save { |user| user.email = user.email.downcase } 
    before_save :create_remember_token 
    before_validation :no_password_omniauth 
    validates_uniqueness_of :name, { case_sensitive: false } 
    validates :name, presence: true, length: { maximum: 50 } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, 
        uniqueness: { case_sensitive: false } 
    validates :password, length: { minimum: 6 } 
    validates :password_confirmation, presence: true 

    @called_omniauth = false 

    def apply_omniauth(omniauth) 
authentications.build(:provider => omniauth['provider'], 
:uid => omniauth['uid'], 
:token => omniauth['credentials'].token, 
:secret => omniauth['credentials'].secret) 
@called_omniauth = true 
self.email = "[email protected]" 
self.name = omniauth['info']['name'] 
self.password = self.password_confirmation = "password" 
end 

    def password_required 
    return false if @called_omniauth == true 
    (authentications.empty? || !password.blank?) 
end 


    def twitter 
unless @twitter_user 
provider = self.authentications.find_by_provider('twitter') 
@twitter_user = Twitter::Client.new(:oauth_token => provider.token, :oauth_token_secret => provider.secret) rescue nil 
end 
@twitter_user 
end 


    private 

    def create_remember_token 
     self.remember_token = SecureRandom.urlsafe_base64 
    end 

    def apply_twitter(omniauth) 
if (extra = omniauth['extra']['user_hash'] rescue false) 
# Example fetching extra data. Needs migration to User model: 
# self.firstname = (extra['name'] rescue '') 
end 
end 

def no_password_omniauth 
    self.password_digest = SecureRandom.urlsafe_base64 unless password_required 
end 


def hash_from_omniauth(omniauth) 
{ 
:provider => omniauth['provider'], 
:uid => omniauth['uid'], 
:token => omniauth['credentials']['token'], 
:secret => omniauth['credentials']['secret'] 
} 
end 
end 

最後,這裏的

class UsersController < ApplicationController 
    before_filter :signed_in_user, 
       only: [:index, :edit, :update, :destroy] 
    before_filter :correct_user, only: [:edit, :update] 
    before_filter :admin_user,  only: :destroy 

def create 
    @user = User.new(params[:user]) 
    if @user.save 
     sign_in @user 
     flash[:success] = "Welcome" 
     redirect_to root_url 
    else 
     render 'new' 
    end 
    end 

    def edit 
    end 
+0

錯誤消息很明顯。您正在嘗試在需要的地方傳遞3個參數。 – 2013-04-11 00:01:15

+0

我該如何解決這個問題?我一直堅持在這一步現在,所以任何幫助,將不勝感激 – user2159586 2013-04-11 00:06:41

回答

0

您已經定義sign_in_and_redirect只接受一個參數的users_controller.rb - 但你傳遞兩個參數英寸

因此,或許sign_in_and_redirect(:user, authentication.user)在您的控制器應該成爲sign_in_and_redirect(authentication.user)

但值得注意的兩件事情:

  • 你sign_in_and_redirect方法實際上並沒有簽署的用戶,它只是重定向他們。
  • 如果您正在使用Devise,那麼您將擁有一個已使用以下簽名定義的名稱的方法:sign_in_and_redirect(resource_or_scope, *args)。我會建議使用他們的方法(如果您使用OmniAuth的Devise)或將您的方法命名爲其他方法以避免衝突和混淆。

[事實上,我分析可能是有點過,給出的堆棧跟蹤指向您sign_in方法,而不是sign_in_and_redirect - 在堆棧跟蹤的行號準確的代碼你分享的,或者有你刪除了一些代碼?]

+0

嘿帕特,我注意到,通過將控制器中的代碼更改爲'sign_in_and_redirect(身份驗證。用戶)'確實使參數消失,並將用戶路由到主頁(未登錄)。你知道他們爲什麼沒有創建賬戶嗎?它之前正在工作,所以不知道改變了什麼 – user2159586 2013-04-11 01:33:06

+0

他們沒有被創建?或者只是沒有登錄? – pat 2013-04-11 03:00:58

+0

他們正在重新創建,但現在我得到了這個錯誤消息:'未定義的方法用戶'爲零:NilClass'。這來自authentications_controller中def create中的'sign_in(authentication.user)'行。應該在哪裏正確定義「用戶」方法? – user2159586 2013-04-11 03:09:50

相關問題