10

मेरे उपयोगकर्ताओं में फोटो एलबम पृष्ठ वे देखे गए फ़ोटो देख रहे हैं और प्रत्येक तस्वीर पर 'डिफ़ॉल्ट बनाएं' लिंक है। जब उपयोगकर्ता make default पर क्लिक करता है, तो फ़ोटो की आईडी को मेरी प्रोफाइल तालिका के photo_id कॉलम में संग्रहीत किया जाता है।मैं उस पृष्ठ पर वापस रीडायरेक्ट कैसे करूं जो मैं वर्तमान में चालू हूं?

मुद्दा उन्हें रीडायरेक्ट हो रहा है वापस करने के लिए:

localhost:3000/settings/photo_gallery/:id 

वहाँ एक रास्ता मैं वापस कि बस डिफ़ॉल्ट के रूप में स्थापित किया गया है तस्वीर की आईडी का उपयोग कर फोटो एलबम रीडायरेक्ट कर सकते हैं है? क्या रेल ढूंढ सकता है कि मैं एक फोटो की आईडी देखकर कौन सा एल्बम रीडायरेक्ट करना चाहता हूं, क्योंकि फोटो एक फोटो एलबम से संबंधित है, और एक फोटो एलबम में कई तस्वीरें हैं?

मेरे पास मेरी डेटाबेस में निम्न तालिकाओं:

  • उपयोगकर्ता (ओं): कई Photoalbums एक प्रोफ़ाइल है, है
  • प्रोफाइल (ओं): उपयोगकर्ता के अंतर्गत आता है
  • PhotoAlbum (ओं): , उपयोगकर्ता के अंतर्गत आता है है कई फोटो
  • फोटो (s):

    : photoAlbum को

नियंत्रक कार्रवाई अंतर्गत आता है

def set_default_profile_photo 

    photo = Profile.find_by_user_id(current_user.id) 
    photo.photo_id = params[:photo_id] 
    photo.save 

    redirect_to "**HERE IS WHERE I'D LIKE TO REDIRECT TO THE PHOTOALBUM THE PHOTO IS IN**" 
    flash[:success] = "Default photo set!" 

end 

मेरे मार्गों:

    users GET /users(.:format)         {:action=>"index", :controller=>"users"} 
          POST /users(.:format)         {:action=>"create", :controller=>"users"} 
       new_user GET /users/new(.:format)        {:action=>"new", :controller=>"users"} 
       edit_user GET /users/:id/edit(.:format)       {:action=>"edit", :controller=>"users"} 
        user GET /users/:id(.:format)        {:action=>"show", :controller=>"users"} 
          PUT /users/:id(.:format)        {:action=>"update", :controller=>"users"} 
          DELETE /users/:id(.:format)        {:action=>"destroy", :controller=>"users"} 
       sessions GET /sessions(.:format)        {:action=>"index", :controller=>"sessions"} 
          POST /sessions(.:format)        {:action=>"create", :controller=>"sessions"} 
       new_session GET /sessions/new(.:format)       {:action=>"new", :controller=>"sessions"} 
      edit_session GET /sessions/:id/edit(.:format)      {:action=>"edit", :controller=>"sessions"} 
        session GET /sessions/:id(.:format)       {:action=>"show", :controller=>"sessions"} 
          PUT /sessions/:id(.:format)       {:action=>"update", :controller=>"sessions"} 
          DELETE /sessions/:id(.:format)       {:action=>"destroy", :controller=>"sessions"} 
       passwords GET /passwords(.:format)        {:action=>"index", :controller=>"passwords"} 
          POST /passwords(.:format)        {:action=>"create", :controller=>"passwords"} 
      new_password GET /passwords/new(.:format)       {:action=>"new", :controller=>"passwords"} 
      edit_password GET /passwords/:id/edit(.:format)      {:action=>"edit", :controller=>"passwords"} 
       password GET /passwords/:id(.:format)       {:action=>"show", :controller=>"passwords"} 
          PUT /passwords/:id(.:format)       {:action=>"update", :controller=>"passwords"} 
          DELETE /passwords/:id(.:format)       {:action=>"destroy", :controller=>"passwords"} 
       profiles GET /profiles(.:format)        {:action=>"index", :controller=>"profiles"} 
          POST /profiles(.:format)        {:action=>"create", :controller=>"profiles"} 
       new_profile GET /profiles/new(.:format)       {:action=>"new", :controller=>"profiles"} 
      edit_profile GET /profiles/:id/edit(.:format)      {:action=>"edit", :controller=>"profiles"} 
        profile GET /profiles/:id(.:format)       {:action=>"show", :controller=>"profiles"} 
          PUT /profiles/:id(.:format)       {:action=>"update", :controller=>"profiles"} 
          DELETE /profiles/:id(.:format)       {:action=>"destroy", :controller=>"profiles"} 
        emails GET /emails(.:format)         {:action=>"index", :controller=>"emails"} 
          POST /emails(.:format)         {:action=>"create", :controller=>"emails"} 
       new_email GET /emails/new(.:format)        {:action=>"new", :controller=>"emails"} 
       edit_email GET /emails/:id/edit(.:format)      {:action=>"edit", :controller=>"emails"} 
        email GET /emails/:id(.:format)        {:action=>"show", :controller=>"emails"} 
          PUT /emails/:id(.:format)        {:action=>"update", :controller=>"emails"} 
          DELETE /emails/:id(.:format)        {:action=>"destroy", :controller=>"emails"} 
        root  /            {:controller=>"users", :action=>"new"} 
        success  /success(.:format)        {:action=>"success", :controller=>"users"} 
        login  /login(.:format)         {:action=>"new", :controller=>"sessions"} 
        logout  /logout(.:format)         {:action=>"destroy", :controller=>"sessions"} 
      reset_password  /reset_password(.:format)       {:action=>"new", :controller=>"passwords"} 
     setup_new_password  /setup_new_password(.:format)      {:action=>"edit", :controller=>"passwords"} 
       settings  /settings(.:format)        {:action=>"settings", :controller=>"users"} 
     settings_account  /settings/account(.:format)      {:controller=>"users", :action=>"account"} 
    settings_edit_profile  /settings/edit_profile(.:format)     {:controller=>"profiles", :action=>"edit_profile"} 
           /:username(.:format)        {:controller=>"users", :action=>"show"} 
      change_password  /change_password(.:format)      {:action=>"change_password", :controller=>"users"} 
profile_photo_set_default  /profile_photo/set_default(.:format)    {:controller=>"photo_albums", :action=>"set_default_profile_photo"} 
    album_photo_set_default  /album_photo/set_default(.:format)    {:controller=>"photo_albums", :action=>"set_default_album_photo"} 
      photo_albums GET /settings/photo_gallery(.:format)     {:action=>"index", :controller=>"photo_albums"} 
          POST /settings/photo_gallery(.:format)     {:action=>"create", :controller=>"photo_albums"} 
      new_photo_album GET /settings/photo_gallery/new(.:format)    {:action=>"new", :controller=>"photo_albums"} 
     edit_photo_album GET /settings/photo_gallery/:id/edit(.:format)  {:action=>"edit", :controller=>"photo_albums"} 
       photo_album GET /settings/photo_gallery/:id(.:format)    {:action=>"show", :controller=>"photo_albums"} 
          PUT /settings/photo_gallery/:id(.:format)    {:action=>"update", :controller=>"photo_albums"} 
          DELETE /settings/photo_gallery/:id(.:format)    {:action=>"destroy", :controller=>"photo_albums"} 
        photos GET /settings/photo_gallery/photos(.:format)   {:action=>"index", :controller=>"photos"} 
          POST /settings/photo_gallery/photos(.:format)   {:action=>"create", :controller=>"photos"} 
       new_photo GET /settings/photo_gallery/photos/new(.:format)  {:action=>"new", :controller=>"photos"} 
       edit_photo GET /settings/photo_gallery/photos/:id/edit(.:format) {:action=>"edit", :controller=>"photos"} 
        photo GET /settings/photo_gallery/photos/:id(.:format)  {:action=>"show", :controller=>"photos"} 
          PUT /settings/photo_gallery/photos/:id(.:format)  {:action=>"update", :controller=>"photos"} 
          DELETE /settings/photo_gallery/photos/:id(.:format)  {:action=>"destroy", :controller=>"photos"} 
+0

यह लिंक आपके लिए उपयोगी हो सकता है। उम्मीद है कि यह मदद करेगा। http://stackoverflow.com/questions/2139996/ruby-on-rails-redirect-toback – piam

उत्तर

20

redirect_to :back मेरे लिए काम किया लेकिन मैं देखना चाहता हूँ कि अगर यह सही विकल्प है http://api.rubyonrails.org/files/actionpack/lib/action_controller/metal/redirecting_rb.html

+1

मुझे सही चीज़ की तरह दिखता है। यदि आपके पास redirect_to: back का उपयोग करने का विकल्प नहीं था, तो आप PhotoAlbum.where (: id => Photo.where (: id => पैराम्स [: photo_id])। Redirect.photo_album_id) के साथ redirect_tbum_id के साथ एक ही परिणाम प्राप्त कर सकते हैं। इस प्रश्न को करने के बेहतर तरीके हैं लेकिन यह काम करना चाहिए। – brad

1

एक परियोजना में हम अस्थायी भंडारण के लिए सत्र का इस्तेमाल किया है, क्योंकि redirect_to :back हमारे लिए काम नहीं किया हमारे पास def new था जहां हमने session[:return_to] = request.referer def create में redirect_to session[:return_to] जोड़ा था। मैं अब और नहीं जानता, इसलिए हम redirect_to :back

+1

जिस कारण से आपने 'redirect_to: back' का उपयोग नहीं किया है, वह सबसे अधिक संभावना है क्योंकि आप ** ** ** ** वापस जाने के इच्छुक हैं, यानी यदि आप 'निर्माण' से 'वापस' गए हैं तो आपको वापस 'नए' पर ले जाया जाएगा यही कारण है कि आप वास्तव में 'नए' से पहले जो भी पहले थे, दो कदम वापस जाना चाहते हैं। –

0

उपयोग नहीं कर सका आप photo_album पेज से आया है, तो आप ऐसा करने में सक्षम होना चाहिए:

redirect_to :back 

अन्यथा, आप एक नामित मार्ग ऐसा करने में सक्षम होना चाहिए जैसे:

redirect_to photo_album_path(photo.album_id) # or whatever the association key is 

बीटीडब्लू, आपके पास photo_galleries पर फोटो_लबम्स मैपिंग क्यों है? यदि आप अपने संसाधनों और मार्गों को एक समान तरीके से नामित करते हैं तो यह बहुत कम भ्रमित है। यानी: यदि आप अपने रूट एंडपॉइंट्स को/photo_galleries का उपयोग करना चाहते हैं तो आपको अपने संसाधन PhotoGallery का नाम देना चाहिए।

redirect_to request.referrer 

आगे के संदर्भ के लिए::

redirect_back(fallback_location: root_path) 

यह वापस अनुप्रेषित करता है जब भी HTTP_REFERER: http://en.wikipedia.org/wiki/HTTP_referer

13

यह तुम क्या चाहते है ज्ञात है। अन्यथा यह fallback_location पर रीडायरेक्ट करता है।

redirect_to :back बहिष्कृत है और रेल 5.1 से हटा दिया जाएगा।

+0

आपने मुझे अनुसंधान का बहुत समय बचाया! धन्यवाद! – SsouLlesS

+0

धन्यवाद @Luis – cyonder

3

रेल 5 में यह समारोह शुरू की गई थी

+0

उपयोगकर्ता को फ्लैश संदेश के साथ कुछ गलत होने के लिए 'redirect_back' कथन के साथ 'नोटिस' ऑब्जेक्ट कैसे पास कर सकता हूं? –

+3

ठीक है, मैं इसे कर कर हल कर सकता हूं: ** 'redirect_back (fallback_location: root_path, नोटिस:" कुछ गड़बड़ हुई! ")' **। उम्मीद है [यह हो सकता है] (http://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_back) किसी भी तरह से मदद करें। –

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

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