2012-02-20 11 views
27

रेल में नियंत्रक "abc" को छोड़कर पहले_फिल्टर वाक्यविन्यास क्या होता है।पहले_फिल्टर वाक्यविन्यास जब आप "को छोड़कर" नियंत्रक "एबीसी"

उदाहरण के लिए, application_controller में अगर मैं कहना चाहता हूँ:

before_filter :login_required :except => ["-name of controller-"] 

पृष्ठभूमि - बस नियंत्रक कि वास्तव में एक उपयोगकर्ता प्रमाणीकृत हो रही संभालती को छोड़कर पूरे एप्लिकेशन में बुनियादी प्रमाणीकरण चाहता था ....

उत्तर

55

आप नियंत्रक में निम्न पंक्ति जहां before_filter निष्पादित नहीं किया जाना चाहिए रख सकते हैं:

skip_before_filter :login_required 

तुम भी कर सकते हैं तरीकों जहां before_filter:only और :except विकल्पों के साथ नजरअंदाज कर दिया है specifiy:

skip_before_filter :login_required, :only => [:login] 

एक उदाहरण here


संपादित करें: रेल 4 के साथ, before_filterbefore_action साथ एलियास है, और skip_before_filter भी साथ skip_before_action

14

before_filter वाक्य रचना एलियास किया गया है

before_filter :login_required, :except => ["-name of the action-"] 

Rails API Doc पर एक नज़र डालें।

3

नियंत्रक नाम का उपयोग करने के बजाय, मैं इस तथ्य का लाभ उठाने की सलाह दूंगा कि नियंत्रक अपने माता-पिता से अपने फ़िल्टर प्राप्त करते हैं।

# app/controllers/application_controller.rb 
class ApplicationController 
    # no filters here 
end 

# app/controllers/authenticated_controller.rb 
class AuthenticatedController < ApplicationController 
    before_filter :login_required 
end 

# app/controllers/some_other_controller.rb 
class SomeOtherController < AuthenticatedController 
    # inherits the before_filter from AuthenticatedController 
    # use this for most of your other controllers 
end 

# app/controllers/unauthenticated_controller.rb 
class UnauthenticatedController < ApplicationController 
    # no filters, since this inherits directly from ApplicationController 
    # use this for the controller that you don't want to check login on 
end 

इसका मतलब यह है नियंत्रकों पता है कि क्या वे लॉगिन जाँच करने के लिए, बल्कि एक (संभवतः भंगुर) नामों की सूची की तुलना में माना जाता कर रहे हैं: तो क्या मैं सलाह देते हैं कुछ इस तरह है।

संबंधित मुद्दे