私は初心者で、omniauth & 2 と omniauth-facebook を使用して自分の Rails アプリに Facebook ログインを追加しようとしています。https://github.com/plataformatec/devise/wiki/OmniAuth:-Overviewの指示に従いました。
しかし、現在、以下のエラーが表示されています。ネットを検索してさまざまな方法を試しましたが、あまり成功していません。何が間違っているのかわかりません。誰かがこれを手伝ってくれますか? とても有難い。
実行中
rails (3.2.12) oauth2 (0.8.1) を使用 omniauth (1.1.4) を使用 omniauth-oauth2 (1.1.1) を使用 omniauth-facebook (1.4.1) を使用
エラー: ActiveRecord::RecordNotFound in UsersController#show ID=sign_up のユーザーが見つかりませんでした
私のコード
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook]
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :name, :provider, :uid, :name
attr_accessible :title, :date_of_birth, :firstName, :lastName, :mailId, :phone, :provider, :uid
protected
def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
user = User.find(:provider => auth.provider, :uid => auth.uid).first
unless user
user = User.create(name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth.info.email,
password:Devise.friendly_token[0,20]
)
end
user
end
def self.new_with_session(params, session)
super.tap do |user|
if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
user.email = data["email"] if user.email.blank?
end
end
end
has_many :pins, :dependent => :destroy
終わり
devise.rb
require 'devise/orm/active_record'
require 'omniauth-facebook'
config.omniauth_path_prefix = "/users/auth"
config.omniauth :facebook, "XXXX", "XXXX", {:client_options => { :ssl => { :verify => false } }}
end
ルート.rb
Dine::Application.routes.draw do
get "home/index"
resources :pins
resources :pin
resources :users
match '/auth/facebook' => 'omniauth#passthru'
devise_scope :user do
get '/users/auth/:provider' => 'users/omniauth_callbacks#passthru'
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } , :strategy_class => OmniAuth::Strategies::Facebook
match 'users/:id' => 'users#show', as: :user
root :to => 'pins#index'
get 'about' => 'pages#about'
get 'weekly' => 'pages#weekly'
get 'shop' => 'pages#shop'
get 'service' => 'pages#service'
get 'privacy' => 'pages#privacy'
get 'test' => 'pages#test'
get 'recipies' => 'pages@recipies'
match 'contact' => 'contact#new', :as => 'contact', :via => :get
match 'contact' => 'contact#create', :as => 'contact', :via => :post
end
end
OmniauthCallbacksController
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def passthru
send(params[:provider]) if providers.include?(params[:provider])
end
protected
def facebook
raise "Implement me for facebook"
end
def twitter
raise "Implement me for twitter"
end
private
def providers
["facebook", "twitter"]
end
end
ユーザーコントローラー
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
@pins = @user.pins.page(params[:page]).per_page(20)
end
end
new.html.erb
<h2>登入</h2>
<%= simple_form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div class="form-inputs">
<%= f.input :email, :required => false, :autofocus => true %>
<%= f.input :password, :required => false %>
<%= f.input :remember_me, :as => :boolean if devise_mapping.rememberable? %>
</div>
<%= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook) %>
<div class="form-actions">
<%= f.button :submit, "Sign in" %>
</div>
<% end %>
<%= render "devise/shared/links" %>