rails wrong number of arguments (2 for 1) for user_signed_in? and current_user

1.7k Views Asked by At

I'm new in Rails and I have been stuck with this error for a few days.

I have installed Devise and Facebook connect without a problem. I could sign in with Facebook but not with devise registration (this is not what caused this error message).

I continue to install anyway a Twitter Connect and I got this message:

Wrong number of argument (2 for 1) in _app_views_shared__navbar_html_erb

on line 25 which corresponds to user_signed_in?

This message appears now on other HTML pages when current_user is mentioned in my HTML.

I can't identify what's wrong with my user attributes. I think something is wrong/missing with Devise, here are my terminal errors:

Started GET "/" for 127.0.0.1 at 2015-08-31 15:46:30 +0200
Processing by HighVoltage::PagesController#show as HTML
Parameters: {"id"=>"home"}
Rendered pages/home.html.erb within layouts/application (7.0ms)
Rendered shared/_navbar.html.erb (3.9ms)
Completed 500 Internal Server Error in 67ms

ArgumentError - wrong number of arguments (2 for 1):
devise (3.5.1) lib/devise.rb:457:in `block (2 levels) in configure_warden!'
warden (1.2.3) lib/warden/session_serializer.rb:34:in `fetch'
warden (1.2.3) lib/warden/proxy.rb:212:in `user'
warden (1.2.3) lib/warden/proxy.rb:318:in `_perform_authentication'
warden (1.2.3) lib/warden/proxy.rb:104:in `authenticate'
devise (3.5.1) lib/devise/controllers/helpers.rb:120:in `current_user'
devise (3.5.1) lib/devise/controllers/helpers.rb:116:in   `user_signed_in?'

Here is my Gemfile:

  ruby "2.2.0"

  gem "rails", "4.2.0"
  gem "pg"
  gem "figaro"
  gem "simple_form"
  gem "country_select"
  gem "high_voltage"
  gem "devise"
  gem "pundit"
  gem "paperclip"
  gem "aws-sdk", "< 2.0"
  gem 'omniauth-facebook'
  gem 'omniauth'
  gem 'omniauth-twitter'

  gem "jquery-rails"
  gem "sass-rails", "~> 5.0"
  gem "uglifier"
  gem "bootstrap-sass"
  gem "font-awesome-sass"

  gem "rails-i18n"
  gem "devise-i18n"
  gem "devise-i18n-views"
  gem 'acts_as_commentable', '~> 4.0.2'
  gem 'acts_as_votable', '~> 0.10.0'

  group :development, :test do
    gem "spring"
    gem "annotate"
    gem "binding_of_caller"
    gem "better_errors"
    gem "quiet_assets"
    gem "pry-byebug"
    gem "pry-rails"
    gem "letter_opener"
  end
  group :production do
    gem "rails_12factor"
    gem "puma"
    gem "rack-timeout"
  end

Here is my OmniauthController:

class Users::OmniauthCallbacksController <      Devise::OmniauthCallbacksController
  def facebook
    user = User.from_omniauth(request.env['omniauth.auth'])
    if user.persisted?
      sign_in_and_redirect user, event: :authentication
      set_flash_message(:notice, :success, kind: 'Facebook') if is_navigational_format?
    else
      session['devise.facebook_data'] = request.env['omniauth.auth']
      redirect_to new_user_registration_url
    end
  end

  def twitter
    user = User.from_omniauth(request.env["omniauth.auth"])
    if user.persisted?
      # flash[:notice] = I18n.t "devise.omniauth_callbacks.success"
      sign_in_and_redirect user, event: :authentication
    else
      session["devise.twitter_uid"] = request.env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
  end

Here is my User Model

class User < ActiveRecord::Base
  acts_as_voter
  # has_many :comments
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable, omniauth_providers: [:facebook, :twitter]

  has_attached_file :picture,
      styles: { large: "500x500>", medium: "300x300>", thumb: "100x100>" }

  validates_attachment_content_type :picture,
    content_type: /\Aimage\/.*\z/

  def self.from_omniauth(auth)
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.email = auth.info.email
      user.password = Devise.friendly_token[0,20]  # Fake password for validation
      user.name = auth.info.name
      user.picture = auth.info.image
      user.token = auth.credentials.token
      user.token_expiry = Time.at(auth.credentials.expires_at)
    end
  end

Here is my Devise Config:

Devise.setup do |config|
  config.mailer_sender = '[email protected]'
  require 'devise/orm/active_record'
  config.skip_session_storage = [:http_auth]
  config.reconfirmable = true
  config.expire_all_remember_me_on_sign_out = true
  config.sign_out_via = :delete
  config.omniauth :facebook, ENV["FB_ID"], ENV["FB_SECRET"], scope: 'email', secure_image_url: 'true', image_size: 'large'
  config.omniauth :twitter, ENV["TWITTER_API_KEY"], ENV["TWITTER_API_SECRET"], secure_image_url: 'true', image_size: 'bigger'

And finally my routes :

Rails.application.routes.draw do

  resources :users, only: [:show, :edit, :update]

  resources :ressources do
    resources :comments
  end
  resources :ressources do
    member do
      put "like", to: "ressources#like"
      put "dislike", to: "ressources#dislike"
    end
  end

  devise_for :users, controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
  # get 'auth/twitter/callback', to: 'user#update'
  # get "/auth/:provider/callback" => "authentications#create"
end

Thank you for your help!

2

There are 2 best solutions below

1
On

I'm not an expert so I can't give you the exact reason why you and me both were having this problem but --

I was getting this line in the application trace, just like you:

warden (1.2.3) lib/warden/session_serializer.rb:34:in `fetch'

After some digging, I figured out the problem was with the current session, so I opened up a new 'incognito' browser window and the error was gone.

You should be able to solve this problem by erasing your cookies and history from your browser.

1
On

from_omniaut is sking for 2 parameters:

def self.from_omniauth(auth)
  where(provider: auth.provider, uid: auth.uid).first_or_create do |user|

You are passing one:

User.from_omniauth(request.env['omniauth.auth'])

You should pass one or accept two.