2016-04-23 35 views
0

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

非常感谢您的帮助。

enter image description here

+2

尝试改变'类MoviesController 7urkm3n

+1

如果它的帮助,你能接受答案thx。 – 7urkm3n

回答

2

您试图通过ActionController打电话。它不可能,就像你建造它一样。

ActionController::Base 
    -ApplicationController #your method in this controller 

ActionController::Base 
    -MoviesController #yr trying to skip it right here 

要跳过它,你必须要继承象下面这样:

ActionController::Base 
-ApplicationController #yr method is here 
    --MoviesController #it will find that method and skip it. 

控制器

# application_controller.rb 
class ApplicationController < ActionController::Base 
end 


# movies_controller.rb 
class MoviesController < ApplicationController 
end 
+0

非常感谢你! – Felix