rails为user_signed_in错误的参数个数(2个为1)?和current_user

时间:2015-08-31 11:41:06

标签: ruby-on-rails ruby-on-rails-4 devise omniauth omniauth-twitter

我是Rails中的新手,我几天都遇到了这个错误。

我安装了Devise和Facebook连接没有问题。我可以使用Facebook登录,但不能使用设计注册(这不是导致此错误消息的原因)。

我继续安装Twitter Connect,我收到了这条消息:

  

错误的参数数量(2表示1)   _app_views_shared__navbar_html_erb

在第25行,对应user_signed_in?

当我的HTML中提到current_user时,此消息现在出现在其他HTML页面上。

我无法识别用户属性的错误。 我觉得Devise有些错误/缺失,这是我的终端错误:

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?'

这是我的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

这是我的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

这是我的用户模型

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

这是我的Devise Config:

Devise.setup do |config|
  config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'
  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'

最后我的路线:

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

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

我不是专家,所以我无法告诉你为什么你和我都遇到这个问题但 -

我在应用程序跟踪中得到了这一行,就像你一样:

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

经过一番挖掘,我发现问题出在本届会议上,所以我开辟了一个新的&#39;隐姓埋名的&#39;浏览器窗口,错误消失了。

您应该可以通过从浏览器中删除Cookie和历史记录来解决此问题。

答案 1 :(得分:0)

from_omniaut正在寻找2个参数:

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

你传递的是:

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

你应该传递一个或接受两个。