我有这个问题
undefined method `devise' for User (call 'User.connection' to establish a connection):Class
这里的每个答案都指出下面的命令没有运行
rails generate devise:install
然而,我实际上完成了安装设计宝石的过程,我添加了omniauth-facebook宝石,它可以工作几次。但是,当我尝试提交并加载我的生产网站时,它会破坏,那就是错误,然后我回到我的测试网站,它也会破坏,无论如何都是完整的代码
的Gemfile:
gem 'rails', '~> 5.0.2'
gem 'puma', '~> 3.0'
gem 'sass', '~> 3.5', '>= 3.5.1'
gem 'sass-rails', '~> 5.0.4'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'autoprefixer-rails', '~> 7.1', '>= 7.1.3'
gem 'jquery-rails'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'devise'
gem 'bootstrap', '~> 4.0.0.beta'
gem 'font-awesome-rails', '~> 4.7', '>= 4.7.0.1'
gem 'country_select'
gem 'carrierwave', '~> 0.11.2'
gem 'carrierwave-base64'
gem 'fog-aws'
gem 'fog'
gem 'figaro', '~> 1.1', '>= 1.1.1'
gem 'acts_as_follower', github: 'tcocca/acts_as_follower', branch: 'master'
gem 'faker', '~> 1.6', '>= 1.6.3'
gem 'acts_as_votable', '~> 0.10.0'
gem 'omniauth'
gem 'omniauth-facebook'
gem 'sprockets-rails', '~> 3.1', '>= 3.1.1'
路线:
Rails.application.routes.draw do
resources :items do
member do
put "vote", to: "items#vote"
end
end
resources :profiles
resources :relationships, only: [:create, :destroy]
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
resources :users do
member do
get :following, :followers
end
end
get 'pages/wish'
devise_scope :user do
authenticated :user do
root to: 'pages#index', as: :authenticated_root
end
unauthenticated do
root 'devise/sessions#new', as: :unauthenticated_root
end
end
namespace 'api' do
namespace 'v1' do
resources :items
end
end
end
devise.rb:这是我添加到Devise.rb的唯一代码
config.omniauth :facebook, ENV["FB_APP_ID"], ENV["FB_APP_SECRET"]
User.rb:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauthable, omniauth_providers: [:facebook]
def has_facebook_linked?
self.provider.present? && self.uid.present?
end
def apply_omniauth(auth)
update_attributes(
provider: auth.provider,
uid: auth.uid
)
end
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.email = auth.info.email
user.provider = auth.provider
user.uid = auth.uid
user.password = Devise.friendly_token[0,20]
end
end
OmniauthCallbacksController:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
if current_user.present?
current_user.apply_omniauth(request.env["omniauth.auth"])
redirect_to edit_user_registration_path, notice: "Facebook Account Linked!"
else
@user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect @user
end
end
end
答案 0 :(得分:4)
我自己解决了,所以我做的是添加这个标签
extend Devise::Models
在我的 user.rb 文件
中user.rb文件看起来像这样
class User < ApplicationRecord
extend Devise::Models
# more code bellow....
end