2015-10-16 92 views
-1

我想配置谷歌的OAuth和我面臨着一個非常愚蠢的錯誤我解決不了: 當我在按鈕點擊登錄主頁上:未定義模型方法4

<% if user_signed_in? %> 
    Signed in as <%= current_user.name %>. Not you? 
    <%= link_to "Sign out", destroy_user_session_path,:method => :delete %> 
<% else %> 
    <%= link_to "Sign in with Google", user_omniauth_authorize_path(:google_oauth2) %> 
<% end %> 

我得到這個錯誤未定義的方法find_for_google_oauth2爲#

它說,這個問題是在該行: @user = user.find_for_google_oauth2(request.env["omniauth.auth"], current_user)

這裏是我的app/controllers/users/omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController 
 

 
    def facebook 
 
    # You need to implement the method below in your model (e.g. app/models/user.rb) 
 
    @user = User.from_omniauth(request.env["omniauth.auth"]) 
 
    if @user.persisted? 
 
     sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated 
 
     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 google_oauth2 
 
    @user = User.find_for_google_oauth2(request.env["omniauth.auth"], current_user) 
 
    if @user.persisted? 
 
     flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => "Google" 
 
     sign_in_and_redirect @user, :event => :authentication 
 
    else 
 
     session["devise.google_data"] = request.env["omniauth.auth"] 
 
     redirect_to new_user_registration_url 
 
    end 
 
    end 
 
end

這裏是我的應用程序/模型/ user.rb:

class User < ActiveRecord::Base 
 
    before_save :default_values 
 
    attr_accessor :delete_photo 
 
    validates :first_name, presence: true 
 
    validates :last_name, presence: true 
 
    # Include default devise modules. Others available are: 
 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
 
    devise :database_authenticatable, :registerable, 
 
     :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:facebook,:google_oauth2] 
 
    has_many :usertoskills 
 
    has_many :skills, through: :usertoskills 
 
    has_many :usertoprojects 
 
    has_many :projects, through: :usertoprojects 
 
    has_many :demands 
 
    has_many :news 
 
    has_attached_file :photo, :styles => { :small => "150x150>", :regular => "300x300>" }, 
 
        :url => "/assets/users/:id/:style/:basename.:extension", 
 
        :path => ":rails_root/public/assets/users/:id/:style/:basename.:extension", 
 
        :default_url => "/assets/users/default/:style/default.jpg" 
 
    validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png'] 
 
    #def name 
 
    # "#{self.first_name.camelize} #{self.last_name.camelize}" 
 
    #end 
 
    def default_values 
 
    if self.first_name && self.last_name 
 
     self.name = "#{self.first_name.camelize} #{self.last_name.camelize}" 
 
    end 
 
    end 
 

 
    def admin 
 
    self.role == 'admin' 
 
    end 
 

 
    def accessible_demands 
 
    if self.role == 'admin' 
 
     @demands = Demand.all 
 
    else 
 
     @demands = [] 
 
     self.projects.each do |p| @demands.concat(p.demands) end 
 
     @demands = Demand.includes(:project).where(projects: { collective: true }, demands: {user_id: self.id}) | @demands 
 
    end 
 
    return @demands 
 
    end 
 

 
    def accessible_projects 
 
    if self.role == 'admin' 
 
     @projects = Project.all 
 
    else 
 
     @projects = self.projects 
 
    end 
 
    return @projects 
 
    end 
 

 
    def accessible_transactions 
 
    if self.role == 'admin' 
 
     @transactions = Transaction.all 
 
    else 
 
     @transactions = Transaction.where(sender_id: self.id) 
 
    end 
 
    end 
 

 
    def self.from_omniauth(auth) 
 
    where(provider: auth.provider, uid: auth.uid).first_or_create do |user| 
 
    user.email = auth.info.email 
 
    user.password = Devise.friendly_token[0,20] 
 
    #user.first_name = auth.info.first_name 
 
    #user.last_name = auth.info.last_name # assuming the user model has a name 
 
    #user.image = auth.info.image # assuming the user model has an image 
 
    end 
 

 
    def self.find_for_google_oauth2(access_token, signed_in_resource=nil) 
 
    data = access_token.info 
 
    user = User.where(:provider => access_token.provider, :uid => access_token.uid).first 
 
    if user 
 
     return user 
 
    else 
 
     registered_user = User.where(:email => access_token.info.email).first 
 
     if registered_user 
 
     return registered_user 
 
     else 
 
     user = User.create(name: data["name"], 
 
      provider:access_token.provider, 
 
      email: data["email"], 
 
      uid: access_token.uid , 
 
      password: Devise.friendly_token[0,20], 
 
     ) 
 
     end 
 
    end 
 
end 
 
end 
 

 
end

預先感謝您的幫助!

+0

你的問題是什麼? – sawa

+0

我如何解決我點擊Google登錄按鈕時得到的錯誤<%if user_signed_in? %> 以<%= current_user.name%>登錄。不是你? <%=的link_to 「登出」,destroy_user_session_path,:方法=>:刪除%> <% else %> <%=的link_to 「在與谷歌」,user_omniauth_authorize_path(:google_oauth2)%> <% end %> – saraf

回答

4

您的電話號碼中有defend不匹配 - 您在def self.from_omniauth(auth)方法後缺少end。下一行的where不正確地縮進,隱藏它。所以實際上,您的find_for_google_oauth2方法正在定義裏面您的from_omniauth方法。

這是Ruby社區約定對於雙空間縮進策略非常嚴格的原因之一。

+0

非常感謝,我所以對於這個愚蠢的問題抱歉 – saraf

+0

saraf:不需要抱歉 - 如果你不問,你不會學習:) – sevenseacat