10

私のフォトアルバムページにはアップロードした写真が表示され、各写真には「デフォルトにする」リンクがあります。 ユーザーがmake defaultをクリックすると、写真のIDがマイプロファイルテーブルのphoto_id列に保存されます。私は、デフォルトとして設定されている写真のIDを使用してフォトアルバムに戻ってリダイレクトすることができる方法はあり私は現在どのページにリダイレクトしていますか?

localhost:3000/settings/photo_gallery/:id 

問題は、バックにそれらをリダイレクトしていますか?レンダリングでは、リダイレクトしたいアルバムを、写真のIDを調べるだけで、写真は写真アルバムに、写真アルバムは写真がたくさんあるので見つけることができますか?

I持って私のデータベースで次の表:

  • ユーザー(秒):1つのプロファイルを有し、持っている多くのPhotoAlbums
  • プロファイル(S):ユーザーに属する
  • フォトアルバム(S):ユーザーに属し、多くの写真
  • 写真(複数可)を持っている:

    :フォトアルバム

コントローラのアクションに属していますの

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を使用するオプションがない場合、redirect_to PhotoAlbum.where(:id => Photo.where(:id => params [:photo_id])。first.photo_album_id)と同じ結果が得られます。このクエリを実行するより良い方法がありますが、これはうまくいくはずです。 – brad

1

redirect_to :backを理由私たちのために働かなかった。 私たちはdef newを持っていました。session[:return_to] = request.refererdef createに追加しました。redirect_to session[:return_to]を追加しました。 は、私はもう知らない私たちは、あなたがPHOTO_ALBUMページから来た場合は、あなたが行うことができるはずredirect_to :back

+1

あなたが 'redirect_to:back'を使用しなかった理由は、あなたが** 2つの**ステップに戻ることを望んでいるためです。つまり、' create'から 'back'に行った場合、' new'に戻されます。だからこそ、あなたは実際には、新しいものの前にあったものに、2つのステップを戻そうとしているのです。 –

0

を使用することができなかった理由は、:

redirect_to :back 

そうでない場合は、名前付きルートを行うことができるはずlike:

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

なぜ、photo_albumsをphoto_galleriesにマッピングしていますか?シンプルな方法でリソースとルートの名前を付けた場合、混乱はほとんどありません。つまり、ルートエンドポイントで/ photo_galleriesを使用する場合は、リソースにPhotoGalleryという名前を付ける必要があります。

3

Railsの5では、それが機能を導入されました。

redirect_back(fallback_location: root_path) 

それはいつでもHTTP_REFERERをバックリダイレクトありません知られている。それ以外の場合はfallback_locationにリダイレクトされます。

redirect_to :backは廃止され、Rails 5.1から削除されます。

+0

'notice'オブジェクトを' redirect_back'ステートメントで渡して、フラッシュメッセージに何か問題があったことをユーザーに伝えるにはどうしたらいいですか? –

+3

さて、私は以下を行うことでこれを解決できました:** 'redirect_back(fallback_location:root_path、notice:"何かが間違っていました!希望(それはできます)(http://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_back)何とか助けてください。 –

関連する問題