您好,我目前正在關注爲我的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:infrom_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 %>
會很感激的任何幫助。
感謝