我想使用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