3

我是 Rails 的新手,我已经被这个错误困扰了几天。

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

无论如何,我继续安装 Twitter Connect 并收到以下消息:

_app_views_shared__navbar_html_erb 中的参数数量错误(2 比 1)

在第 25 行,对应于user_signed_in?

current_user当我的 HTML 中提到时,此消息现在出现在其他 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.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

谢谢您的帮助!

4

2 回答 2

1

我不是专家,所以我不能告诉你你和我都遇到这个问题的确切原因,但是——

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

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

经过一番挖掘,我发现问题出在当前会话上,所以我打开了一个新的“隐身”浏览器窗口,错误消失了。

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

于 2016-01-27T23:01:46.443 回答
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'])

你应该通过一个或接受两个。

于 2015-08-31T11:45:04.900 回答