我想使用application_controller.rb的before_action,然後一些skip_before_action
s到防止一些網站在登錄用戶之前被調用。Ruby on Rails的skip_before_action在我的應用程序沒有影響
但定義函數在我的application_controller.erb不叫......
application_controller.erb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
layout "application"
before_action :user_logged_in, :set_locale
private
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
# Prüft ob ein Nutzer eingeloggt ist.
def user_logged_in
puts "HA"
if session[:user_id].nil?
flash[:error] = "error"
redirect_to :controller => :startsites, :action => :index
else
flash[:error] = "ok"
redirect_to :controller => :startsites, :action => :index
end
end
end
賣出期權「HA」在user_logged_in
沒有在我的服務器控制檯打印。所以我認爲這個函數還沒有被調用,但是爲什麼呢?
而且在某些控制器我試圖用這樣的:
class MoviesController < ActionController::Base
skip_before_action :user_logged_in, only: [:index, :show]
also not working ... why?
非常感謝您的幫助。
嘗試改變'類MoviesController
7urkm3n
如果它的幫助,你能接受答案thx。 – 7urkm3n