Ruby on rails ArgumentError,创建操作中的参数数错误(3对1)

Ruby on rails ArgumentError,创建操作中的参数数错误(3对1),ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,我使用omniauth gem允许用户注册/登录我的rails应用程序 这是我档案的一部分 gem 'omniauth' gem 'omniauth-twitter' gem 'twitter' Omniauth=is v1.1.4, Omniauth twtter为v0.0.16, Omniauth oauth是v1.0.1, 推特是4.6.2 我遇到了一个类似的错误,因此我尝试卸载Omniauth并将其降级到版本“~>0.2.6”,但我收到了以下消息: 捆绑包当前已将omniauth锁定在

我使用omniauth gem允许用户注册/登录我的rails应用程序

这是我档案的一部分

gem 'omniauth'
gem 'omniauth-twitter'
gem 'twitter'
Omniauth=is v1.1.4, Omniauth twtter为v0.0.16, Omniauth oauth是v1.0.1, 推特是4.6.2

我遇到了一个类似的错误,因此我尝试卸载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'
这是我的身份验证\u 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
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
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
这是我的会话\u 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
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
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
class sessioncontroller
这是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+\-.]+@[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 = "test@example.com"
 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用户omniauth['provider'],
:uid=>omniauth['uid'],
:token=>omniauth['credentials']。token,
:secret=>omniauth['credentials'].secret)
@称为_omniauth=true
self.email=”test@example.com"
self.name=omniauth['info']['name']
self.password=self.password\u confirmation=“password”
结束
需要def密码
如果@called_omniauth==true,则返回false
(authentications.empty?| |!password.blank?)
结束
def推特
除非@twitter\u用户
provider=self.authentications.find_by_provider('twitter'))
@twitter\u user=twitter::Client.new(:oauth\u token=>provider.token,:oauth\u token\u secret=>provider.secret)rescue nil
结束
@推特用户
结束
私有的
def创建\u记住\u标记
self.memory_token=SecureRandom.urlsafe_base64
结束
def apply_twitter(omniauth)
if(extra=omniauth['extra']['user\u hash']rescue false)
#获取额外数据的示例。需要迁移到用户模型:
#self.firstname=(额外的['name']rescue'')
结束
结束
def无密码认证
self.password\u digest=SecureRandom.urlsafe\u base64除非需要密码
结束
def hash_from_omniauth(omniauth)
{
:provider=>omniauth['provider'],
:uid=>omniauth['uid'],
:token=>omniauth['credentials']['token'],
:secret=>omniauth['credentials']['secret']
}
结束
结束
最后,这里是users\u 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
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
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
class UsersController
您已经定义了sign\u in\u和\u redirect以只接受一个参数,但您正在传入两个参数

因此,控制器中的
登录和重定向(:user,authentication.user)
应该变成
登录和重定向(authentication.user)

但有两件事值得注意:

  • 您的sign\u-in\u和\u-redirect方法实际上并没有让用户登录,它只是重定向用户
  • 如果您使用的是Deviate,那么您将有一个已使用以下签名定义了该名称的方法:
    sign\u in\u和\u redirect(resource\u或\u scope,*args)
    。我建议您要么使用他们的方法(如果您在OmniAuth中使用Deviate),要么将您的方法命名为其他名称,以避免冲突和混淆

[实际上,我的分析可能有点不准确,因为堆栈跟踪指向您的sign\u-in方法,而不是sign\u-in\u和\u-redirect-堆栈跟踪中的行号对于您共享的代码是否准确,或者您是否删除了一些代码?]

错误消息很清楚。您正在尝试传递3个参数,而它期望传递一个参数。如何修复此问题?我已经在这一步上停留了一段时间,因此任何帮助都会得到赞赏。嘿,帕特,我注意到,通过将控制器中的代码更改为
sign\u in\u和\u redirect(authentication.user)
会使参数消失,并将用户路由到主页(未登录)。你知道为什么他们的账户没有被创建吗?它以前是工作的,所以不确定它们不会被创造出什么变化?或者只是没有登录?它们将再次被创建,但现在我收到以下错误消息:
undefined method user'for nil:NilClass
。这来自authentications\u控制器中def create内的“登录”(authentication.user)
行。方法“user”应该在哪里正确定义?我不确定-你能更新你问题中的代码文件以匹配它们的当前状态吗?我在最后一步。。。我正在尝试实际登录用户。你是对的,它只是重定向他们。如果我没有使用Deviate,那么应该如何定义
登录和重定向
,以及在何处?这真的会帮我很多,所以请让我知道