私は RoR コミュニティに比較的慣れていないので、元の Devise または Omniauth-Facebook を介して認証した後、各ユーザーの個人プロファイルを作成しようとしています。認証システムを実装した後、どこに行くべきかについて、私はちょっと行き詰まっています。
サインアップ/サインイン後、ログインしているユーザーの名前を表示する個人ページにリダイレクトして、少しリードしてください。プロファイルとビューの新しいモデルを作成しようとしましたが、うまくいきませんでした。
私の最終的な目標は、各ユーザーのプロフィール ページに、ユーザーがアップロードした名前、写真、テキストを表示することです...
ここに私のコード行があります:
user.rb ユーザーモデル
class User < ActiveRecord::Base
has_many :authentications
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :omniauthable,
:recoverable, :rememberable, :trackable, :validatable,
:omniauth_providers => [:facebook]
attr_accessible :email, :password, :password_confirmation
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.email = auth.info.email
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
def self.new_with_session(params, session)
if session["devise.user_attributes"]
new(session["devise.user_attributes"], without_protection: true ) do |user|
user.attributes = params
user.valid?
end
else
super
end
end
def password_required?
super && provider.blank?
end
def email_required?
super && provider.blank?
end
end
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all user = User.from_omniauth(request.env["omniauth.auth"])
if user.persisted?
sign_in_and_redirect user, notice: "Signed in!"
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
終了 alias_method :facebook, :all 終了
アプリケーションコントローラー
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
ルート.rb
Rails.application.routes.draw do
devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
end
最後にgemファイル
source 'https://rubygems.org'
ruby '2.1.4'
gem 'rails', '4.2.0.beta4'
gem 'bcrypt', '3.1.7'
gem 'faker', '1.4.2'
gem 'carrierwave', '0.10.0'
gem 'mini_magick', '3.8.0'
gem 'fog', '1.23.0'
gem 'will_paginate', '3.0.7'
gem 'bootstrap-will_paginate', '0.0.10'
gem 'bootstrap-sass', '3.2.0.0'
gem 'sass-rails', '5.0.0.beta1'
gem 'uglifier', '2.5.3'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '4.0.0.beta2'
gem 'turbolinks', '2.3.0'
gem 'jbuilder', '2.2.3'
gem 'rails-html-sanitizer', '1.0.1'
gem 'sdoc', '0.4.0', group: :doc
gem 'devise', github: 'plataformatec/devise'
gem 'omniauth'
gem 'omniauth-twitter'
gem 'omniauth-facebook'
gem 'omniauth-linkedin'
gem 'figaro'
gem 'protected_attributes'
サインイン/サインアップ時にリダイレクトをカスタマイズするには、rails generate devise:controllers を実行する必要がありますか? または、それらすべてを管理するために、コントローラーとモデルを個別に作成することは可能ですか?
前もって感謝します!