2013-04-11 10 views
15

यहां मैं प्रमाणीकरण के लिए डेविस जेम का उपयोग कर रहा हूं। अगर कोई लॉगिन के बिना पेज खोलना चाहता है तो यह साइन_इन पेज पर रीडायरेक्ट करता है और उस पृष्ठ पर वापस साइन इन करने के बाद जो उपयोगकर्ता खोलने का प्रयास करता है। मैं अपनी समस्या के लिए Redirect loop with Devise after_sign_in_path_for लिंक का उपयोग करता हूं लेकिन यह मेरे लिए काम नहीं करता है।साइन इन या साइन अप के बाद मूल स्थान पर वापस रीडायरेक्ट करें?

def after_sign_in_path_for(resource) 
    params[:next] || super 
end 

यह मुझे उस पृष्ठ पर वापस रीडायरेक्ट नहीं करता है जिसे मैं खोलना चाहता हूं। उदाहरण के लिए : यदि मैं "127.0.0.1:3000/post/2/edit" खोलना चाहता हूं, तो यह साइन इन करने के बाद इस पृष्ठ पर वापस नहीं आ जाता है।

उत्तर

33

आधिकारिक रेपो/विकी/मुद्दों, और फिर SO। आपके द्वारा प्राप्त उत्तर पुराना है।

यहाँ जवाब है: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-back-to-current-page-after-sign-in,-sign-out,-sign-up,-update

बस ApplicationController में निम्नलिखित जोड़ें

after_filter :store_location 

def store_location 
    # store last url as long as it isn't a /users path 
    session[:previous_url] = request.fullpath unless request.fullpath =~ /\/users/ 
end 

def after_sign_in_path_for(resource) 
    session[:previous_url] || root_path 
end 
+1

धन्यवाद @Billy आपकी प्रतिक्रिया के लिए .. इस कोड का उपयोग करते हुए, यह मुझे हमेशा root_path (मुख पृष्ठ) और विशिष्ट पेज जो मैं पहले लॉगिन – Rajeev

+0

@rubydev खोला में नहीं करने के लिए, पुन: निर्देशित कर आप Firefox और ऐड-ऑन करने के लिए उपयोग रेफरर अक्षम करें? डीबग टूल का भी उपयोग करें, कहें कि क्या हुआ, यह जांचने के लिए टेम्पलेट में '<% = डीबग सत्र =>' जोड़ें या रेल कंसोल जोड़ें। –

+4

धन्यवाद @ बिली, अब यह काम कर रहा है, यहां केवल एक बदलाव यह है कि मैं after_filter को पहले_फिल्टर – Rajeev

0

मैं एक ही संदेह है और पाया यह, यह भी

def after_sign_in_path_for(resource_or_scope) 
    session.fetch 'user_return_to', admin_root_path 
end 
17

आप डॉन 'की कोशिश इस सब कोड की जरूरत नहीं है। Devise (नवीनतम संस्करणों के रूप में) पहले से ही आपके लिए स्थान बचाता है।

बस का उपयोग करें:

def after_sign_in_path_for(resource) 
    request.env['omniauth.origin'] || stored_location_for(resource) || root_url 
end 

यह नवीनतम omniauth.source, या stored_location करने के लिए उपयोगकर्ता रीडायरेक्ट करेगा और पिछले मामले, जड़ यूआरएल है।

मैंने सोचा कि मुझे उस विधि को बनाने की आवश्यकता है, लेकिन डेविस पहले से ही ऐसा करता है।

स्रोत: https://github.com/plataformatec/devise/wiki/How-To:-redirect-to-a-specific-page-on-successful-sign-in

2

Devise 4 के रूप में, यह मूल मेरे लिए काम किया:

वसीयत स्वचालित रूप से साइन इन पर रीडायरेक्ट और के रूप में आप की वसीयत का उपयोग कर वर्तमान पृष्ठ के स्थान की दुकान के रूप में लंबे लिए साइन अप store_location_for(resource)। ऐसा करने के लिए, ApplicationController app/controllers/application_controller.rb में संपादित करें। जोड़ें:

private 
    # override the devise method for where to go after signing out because theirs 
    # always goes to the root path. Because devise uses a session variable and 
    # the session is destroyed on log out, we need to use request.referrer 
    # root_path is there as a backup 
    def after_sign_out_path_for(resource) 
    request.referrer || root_path 
    end 
3

के रूप में आधिकारिक दस्तावेज में बताया, simpler solution बस अपने application_controller.rb में जोड़ने के लिए होगा:

# saves the location before loading each page so we can return to the 
# right page. If we're on a devise page, we don't want to store that as the 
# place to return to (for example, we don't want to return to the sign in page 
# after signing in), which is what the :unless prevents 
before_filter :store_current_location, :unless => :devise_controller? 

private 
    # override the devise helper to store the current location so we can 
    # redirect to it after loggin in or out. This override makes signing in 
    # and signing up work automatically. 
    def store_current_location 
    store_location_for(:user, request.url) 
    end 

से साइन आउट रीडायरेक्ट बनाने के लिए ApplicationController के लिए निम्न जोड़ें :

class ApplicationController < ActionController::Base 


private 

# If your model is called User 
def after_sign_in_path_for(resource) 
    session["user_return_to"] || root_path 
end 

महत्वपूर्ण नोट (जो मैं यह भी अनदेखा है) यह है कि इस काम के लिए आपको authenticate_user! विधि को कॉल करने की आवश्यकता होगी, जो आपके नियंत्रक के before_action: में डेविस में डिफ़ॉल्ट रूप से उपलब्ध है।यह store_location_for को out of the box in Devise पर कॉल करेगा, और बाकी को उपरोक्त कोड application_controller.rb में संभाला जाएगा, इस प्रकार अनुरोध यूआरएल को सहेजने के लिए कोड को फिर से लिखने की आवश्यकता को समाप्त कर दिया जाएगा।

+1

में' रिमोट = सच्चा 'विकल्प बंद नहीं किया है, यह समाधान' रिमोट: सच्चे 'के साथ-साथ बिना काम करता है, जबकि @ बिली चैन का समाधान केवल बिना काम करता है। – Vadim

0

यदि आपके लॉगिन फॉर्म का अपना पृष्ठ है, तो इसके विपरीत कुछ अन्य समाधान काम नहीं कर सकते हैं, उदाहरण के लिए, प्रत्येक पृष्ठ के शीर्षलेख में लॉगिन फ़ॉर्म। लॉग इन करने के बाद, उपयोगकर्ता को दो पृष्ठों पर वापस जाना होगा, केवल एक ही नहीं।

डेविस के पास Redirecting back to the current page after sign in, sign out, update पर अच्छा तरीका है, जिससे नीचे दिया गया कोड आता है।

सत्र में मूल URL संग्रहीत करना सबसे अच्छा विकल्प है। दो पृष्ठों पर वापस जाने की उपरोक्त समस्या को हल करने के अलावा, "कई ब्राउज़र [request.referer] शीर्षलेख नहीं भेजते हैं। इसलिए इस कार्यक्षमता को लागू करने का एकमात्र मजबूत क्रॉस-ब्राउज़र तरीका सत्र का उपयोग करके है।"

सत्र में यूआरएल संग्रहीत करते समय, किसी भी पोस्ट, पुट, या डिलीट अनुरोध के लिए यूआरएल स्टोर नहीं करना महत्वपूर्ण है, न ही कोई एक्सएचआर अनुरोध, यानी कुछ भी नहीं जिसे उपयोगकर्ता को वास्तव में रीडायरेक्ट नहीं किया जा सकता है।

ध्यान दें कि साइन आउट करने के बाद, उपयोगकर्ता का सत्र नष्ट हो गया है, इसलिए संग्रहीत यूआरएल चला गया है। इस मामले में, उपयोगकर्ता को request.referer पर वापस भेजा जा सकता है। यह स्वीकार्य लगता है क्योंकि अधिकांश वेबसाइटों पर प्रत्येक पृष्ठ पर एक साइन आउट लिंक होता है, इसलिए रेफरर पर लौटने से वास्तव में काम किया जाएगा।

class ApplicationController < ActionController::Base 
    before_action :store_user_location!, if: :storable_location? 
    before_action :authenticate_user! 

    private 
    def storable_location? 
     request.get? && is_navigational_format? && !devise_controller? && !request.xhr? 
    end 

    def store_user_location! 
     store_location_for(:user, request.fullpath) 
    end 

    def after_sign_in_path_for(resource_or_scope) 
    stored_location_for(resource_or_scope) || super 
    end 

    def after_sign_out_path_for(resource_or_scope) 
    request.referrer || super 
    end 
end 
संबंधित मुद्दे