2016-04-23 58 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