我仍然在標題中收到「錯誤」消息,不知道如何解決該問題。 在ApplicationController中,Rails 3 - 過濾器鏈暫停爲:身份驗證呈現或重定向
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :mailer_set_url_options
helper_method :current_user_session, :current_user
def mailer_set_url_options
ActionMailer::Base.default_url_options[:host] = request.host_with_port
end
private
def current_user_session
logger.debug "ApplicationController::current_user_session"
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
logger.debug "ApplicationController::current_user"
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
def authentication
logger.debug "ApplicationController::authentication"
unless current_user
store_location
flash[:warning] = "You must be logged out to access this page"
redirect_to root_url
return false
end
end
def store_location
session[:return_to] = request.url
end
end
在
routes.rb中
#match 'set_activity_account/:id/:value' => 'users#account_activity', :as => :set_activity_account -- this doesn't work as well..
resources :users do
member do
get :action_a, :action_b
end
collection do
get 'account_activity'
end
end
和UsersController
class UsersController < ApplicationController
before_filter :authentication, only: [:index, :edit, :update, :destroy, :action_a, :action_b]
#skip_before_filter :authentication, :only => [:account_activity] didn't help as well
def account_activity
unless current_user.nil?
puts 'A'
if params[:user_id] && params[:status]
puts 'B'
User.find(params[:user_id]).update_attributes(:active => params[:status])
flash[:notice] = 'Activity was successfully changed.'
end
end
redirect_to :back
end
...
始終時被更新的活性屬性,我會得到的消息 重定向本地主機:3000/ 過濾器鏈停止爲:渲染或重定向認證
編輯:從日誌文件中添加了產量:
Started GET "https://stackoverflow.com/users/account_activity?user_id=31&status=0" for 127.0.0.1 at 2012-09-28 00:40:10 +0200
Processing by UsersController#account_activity as HTML
Parameters: {"user_id"=>"31", "status"=>"0"}
ApplicationController::current_user
ApplicationController::current_user_session
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."persistence_token" = '...' LIMIT 1
(0.1ms) BEGIN
(0.7ms) UPDATE "users" SET "last_request_at" = '2012-09-27 22:40:10.258152', "perishable_token" = '...', "updated_at" = '2012-09-27 22:40:10.259093' WHERE "users"."id" = 31
(0.7ms) COMMIT
ApplicationController::current_user_session
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", "31"]]
(0.1ms) BEGIN
User Exists (0.6ms) SELECT 1 FROM "users" WHERE ("users"."email" = '[email protected]' AND "users"."id" != 31) LIMIT 1
(0.5ms) UPDATE "users" SET "active" = 0, "perishable_token" = '...', "updated_at" = '2012-09-27 22:40:10.267227' WHERE "users"."id" = 31
(0.7ms) COMMIT
Redirected to http://localhost:3000/users/31/edit
Completed 302 Found in 19ms (ActiveRecord: 4.5ms)
Started GET "https://stackoverflow.com/users/31/edit" for 127.0.0.1 at 2012-09-28 00:40:10 +0200
Processing by UsersController#edit as HTML
Parameters: {"id"=>"31"}
ApplicationController::authentication
ApplicationController::current_user
ApplicationController::current_user_session
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."persistence_token" = '...' LIMIT 1
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 31 LIMIT 1
Redirected to http://localhost:3000/
Filter chain halted as :authentication rendered or redirected
Completed 302 Found in 5ms (ActiveRecord: 1.3ms)
我怎樣才能解決這個問題?我試圖搜索谷歌在這裏,所以,但不幸的是,仍然不知道,如何解決它..
您需要展示更多關於發生的事情,例如請求來自何處和/或錯誤日誌。我的猜測是,「redirect_to:back」正在向您的應用程序發送before_filter列表中的某個操作,用於:控制器頂部的身份驗證。 – rossta
@rossta我更新我的OP - 從日誌添加輸出。是的,我將應用程序重定向到'before_filter'列表中的動作,即編輯動作。但是對於'edit'這個動作,應用程序也會從'update'動作重定向 - 並且這很好。此外,如果我刪除重定向,然後手動設置我的應用程序的「家庭網址」,那麼我得到相同的錯誤... – user984621