我有一個控制器PostsController,它允許用戶在登錄之前創建帖子。但要保存它,用戶必須使用Omniauth登錄。before_filter從另一個控制器的方法
在PostsController,我有:
class PostsController < ApplicationController
before_filter :authenticate_user_by_service, :only => :create
def create
...
end
private
def authenticate_user_by_service
redirect_to user_omniauth_authorize_path(:facebook)
end
現在,我還有一個控制器來從Facebook,這就是所謂ServicesController
處理回調class ServicesController < ApplicationController
def create
auth = request.env["omniauth.auth"]
... authentication logic here ...
sign_in(:user, service.user)
end
method_alias: :facebook, :create
通常情況下,進行身份驗證,之後,籤我將重定向到:返回。
但是,這裏使用services#create
作爲before_filter。在這種情況下,我應該怎麼做才能讓它回到我的帖子#創建?
更新:我得到這個警告稱該過濾器鏈就在我指的是一個不同的方法
Started POST "/posts" for 127.0.0.1 at 2013-02-26 23:47:41 -0500
Processing by PostsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"H0as8=", "post"=>{"post"=>"bla bla"}, "commit"=>"Create Post"}
Redirected to http://localhost:3000/users/auth/facebook
Filter chain halted as :authenticate_user_by_service rendered or redirected
非常感謝您的詳細解答。你能否解釋一下在帖子中發生了什麼#如果用戶沒有登錄,請創建?我應該在這個方法中調用require_login嗎? – AdamNYC 2013-02-27 15:41:02