2013-04-15 49 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

希望你会知道的名字是否在对象或没有上市。

相关问题