我正在创建一个简单的 Ruby on Rails 应用程序。它允许用户从应用程序登录到 facebook,并在成功登录后返回应用程序主页。我遵循了一些关于 rails casts 和http://blog.yangtheman.com/2012/02/09/facebook-connect-with-rails-omniauth-devise/的教程。但现在我收到http 400 错误。我已经安装了 gemsomniauth、omniauth facebook 和 devise。请帮忙。我正在发布相同的视图、模型和控制器。我的应用程序已经包含与 twitter 的集成。
Index.html.erb(这个可以说是应用的首页)
<h1>Twitter tatter</h1>
<form action="create" method="post">
<label for="keyword">Enter_keyword</label>
<input id="keyword" name="tweet[search]" size="30" type="text" />
<input type="submit" value="search" />
<%= link_to 'Login with Facebook', '/auth/facebook/' %>
<!--
<a href="/auth/facebook" class="auth_provider">
<%= image_tag "facebook_64.png", :size => "64x64", :alt => "Login_to_Facebook "%>Facebook
</a>
</br>
-->
</form>
</br></br></br></br></br>
<div id="container">
<% if (@tweets != nil && @tweets.count>0) then %>
User.rb(使用设计创建的模型)
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
has_many :authentications # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
认证模型
class Authentication < ActiveRecord::Base
belongs_to :user
end
身份验证控制器
def create
auth = request.env["omniauth.auth"]
authentication = Authentication.find_by_provider_and_uid(auth['provider'], auth['uid'])
flash[:notice] = "Signed in successfully."
sign_in_and_redirect(:user, authentication.user)
end
路由.rb
NewYearTweets::Application.routes.draw do
devise_for :users
resources :authentications
resources :tweetscontroller
get "tweets/index"
match 'tweets/create' => 'tweets#create'
match '/auth/:facebook/callback' => 'authentications#create
我正在发布与 facebook 集成相关的代码,而不是与 twitter 集成相关的代码,因为它工作正常。成功登录facebook后,我想重定向到主页即索引页