[英]ArgumentError, wrong number of arguments (3 for 1) in create action
I'm using the omniauth gem to allow users to signup/login to my rails app 我正在使用omniauth gem来允许用户注册/登录我的Rails应用
Here's a part of my gemfile 这是我的宝石文件的一部分
gem 'omniauth'
gem 'omniauth-twitter'
gem 'twitter'
Omniauth =is v1.1.4, Omniauth-twtter is v0.0.16, Omniauth-oauth is v1.0.1, Twitter is 4.6.2 Omniauth =是v1.1.4,Omniauth-twtter是v0.0.16,Omniauth-oauth是v1.0.1,Twitter是4.6.2
I came across this thread with a similar error wrong number of arguments (3 for 1) after upgrading rails from 3.1.1 to 3.1.3 so I tried to uninstall Omniauth and downgrade to version "~> 0.2.6" but I got this message: 在将Rails从3.1.1升级到3.1.3之后,我遇到了一个类似的错误错误的参数数目错误(3为1),所以我尝试卸载Omniauth并将其降级到版本“〜> 0.2.6”,但是我明白了信息:
The bundle currently has omniauth locked at 1.1.4. 该捆绑包当前的omniauth锁定为1.1.4。 Is the omniauth version the cause of the issue or something else?
omniauth版本是问题的原因还是其他原因?
The error message that I'm trying to fix is this when I try to use Twitter signin 我尝试修复的错误消息是当我尝试使用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'
Here's my authentications_controller.rb 这是我的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
Here's my sessions_helper.rb 这是我的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
Here's my sessions_controller.rb 这是我的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
Here's the user.rb model 这是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
Finally, here's the users_controller.rb 最后,这是users_controller.rb
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
You've defined sign_in_and_redirect to accept only one argument - but you're passing two arguments in. 您已将sign_in_and_redirect定义为仅接受一个参数-但您要传入两个参数。
So perhaps sign_in_and_redirect(:user, authentication.user)
in your controller should become sign_in_and_redirect(authentication.user)
. 因此,也许
sign_in_and_redirect(:user, authentication.user)
应该成为sign_in_and_redirect(authentication.user)
。
But it's worth noting two things: 但值得注意的是两件事:
sign_in_and_redirect(resource_or_scope, *args)
. sign_in_and_redirect(resource_or_scope, *args)
。 I would recommend either using their method (if you're using Devise with OmniAuth) or naming your method something else to avoid conflicts and confusion. [Actually, my analysis may be a little off, given the stack trace points to your sign_in method, not sign_in_and_redirect - are the line numbers in the stack trace accurate for the code you've shared, or have you removed some code?] [实际上,考虑到您的sign_in方法的堆栈跟踪点,而不是sign_in_and_redirect,我的分析可能会有些偏离-堆栈跟踪中的行号对于您共享的代码是否准确,还是您删除了一些代码?]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.