2013-04-15 110 views
0

您好,我目前正在關注爲我的webapp添加Facebook身份驗證的railscast 360。它是一切正常,直到我開始收到此錯誤:SessionsController中的NoMethodError#使用Omniauth Facebook身份驗證創建railscast 360

"NoMethodError in SessionsController#create

undefined method `name=' for #"

app/models/user.rb:6:in block in from_omniauth' app/models/user.rb:3:in tap' app/models/user.rb:3:in from_omniauth' app/controllers/sessions_controller.rb:3:in create'

我已經看過這個網站,但他們中的很多在幾個不同的答案似乎是Twitter的認證,我只是試圖讓Facebook的加工。

我已經在Facebook Developers上創建了一個應用程序,並完全遵循了軌道演員教程。我非常感謝這方面的幫助。

到目前爲止我的代碼是

user.rb:

class User < ActiveRecord::Base 
def self.from_omniauth(auth) 
    where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user| 
    user.provider = auth.provider 
    user.uid = auth.uid 
    user.name = auth.info.name 
    user.oauth_token = auth.credentials.token 
    user.oauth_expires_at = Time.at(auth.credentials.expires_at) 
    user.save! 
    end 
end 
end 

application_controller.rb

class ApplicationController < ActionController::Base 
    protect_from_forgery 

    private 

    def current_user 
    @current_user ||= User.find(session[:user_id]) if session[:user_id] 
    rescue ActiveRecord::RecordNotFound 
end 
    helper_method :current_user 
end 

session_controller.rb

class SessionsController < ApplicationController 
    def create 
    user = User.from_omniauth(env["omniauth.auth"]) 
    session[:user_id] = user.id 
    redirect_to root_url 
    end 

    def destroy 
    session[:user_id] = nil 
    redirect_to root_url 
    end 
end 

這是我的我的application.html.erb內部代碼:

<div id="user_nav"> 
       <% if current_user %> 
        Signed in as <strong><%= current_user.name %></strong>! 
        <%= link_to "Sign out", signout_path, id: "sign_out" %> 
       <% else %> 
       <%= link_to "Sign in with Facebook", "/auth/facebook", id: "sign_in" %> 
       <% end %> 

會很感激的任何幫助。

感謝

回答

0

去了你的調用堆棧跟蹤的(它是在調用的相反順序),它看起來像你的session_controller電話user模型,特別是用戶塊。

看看你的代碼,沒有錯誤突出,但堆棧跟蹤標記行6並提到它不明白的方法:undefined method 'name='。如果我不得不採取的猜測,很可能有表中沒有名稱的列 - 這可能不是解決你的問題,但嘗試以下方法:

$ rake db:migrate

$ rails c

然後在rails控制檯中,嘗試檢查用戶對象的字段。

> User.new

希望你會知道的名字是否在對象或沒有上市。

相關問題