SessionsController#中的NoMethodError使用Omniauth Facebook身份验证railscast 360创建

时间:2013-04-15 17:16:49

标签: ruby-on-rails ruby facebook omniauth nomethoderror

大家好,我目前正在跟踪railscast 360,将Facebook身份验证添加到我的webapp中。在我开始收到此错误之前一切顺利:

  

“SessionsController中的NoMethodError #create

     

未定义的方法`name ='for#“

     

app / models / user.rb:6:block in from_omniauth' app/models/user.rb:3:in点按'   app / models / user.rb:3:from_omniauth' app/controllers/sessions_controller.rb:3:in创建'

我在这个网站上看了几个不同的答案,但其中很多似乎都是针对Twitter认证的,我只是想让Facebook工作。

我已经在Facebook开发者上创建了一个应用程序,并完全遵循了轨道演员的教程。我非常感谢你们的帮助。

到目前为止,我的代码是

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 %>

非常感谢任何帮助。

由于

1 个答案:

答案 0 :(得分:0)

关闭你的调用堆栈跟踪(它与调用的顺序相反),好像是你的session_controller调用user模型,特别是用户块。

查看您的代码,没有错误突出,但堆栈跟踪标记第6行并提到它不理解方法:undefined method 'name='。如果我不得不猜测,表中可能没有名称栏 - 这可能无法解决您的问题,但请尝试以下方法:

$ rake db:migrate

$ rails c

然后在rails控制台中,尝试检查 User 对象的字段。

> User.new

希望您知道对象中是否列出了名称。