-1
類
我有這個問題未定義的方法`色器件」的用戶(稱之爲‘User.connection’建立連接):
undefined method `devise' for User (call 'User.connection' to establish a connection):Class
這裏的每一個回答下面沒有運行
指出的命令rails generate devise:install
但是我實際上完成了安裝devise gem的過程,我也添加了omniauth-facebook gem,它可以工作幾次。但是當我嘗試提交併載入我的生產現場折斷和多數民衆贊成在錯誤然後我回到我的測試網站它打破太反正繼承人的完整代碼
的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
這不是一個結構良好的問題。請參閱:[mcve]。你的代碼是什麼?你預計會發生什麼?基於上述情況,它看起來像你只是調用'User.devise' - 這不是圖書館的工作方式,所以我不知道你在期待什麼。 –
編輯只是增加了更多的內容澄清 –