11

link_to にリダイレクトを含める方法はありますか? 削除後に現在のページを更新したいだけです。ただし、アプリの複数のビューから同じレコードを削除できます。

これは link_to です:

<%= link_to 'Delete', expense, confirm: 'Are you sure?', method: :delete, :class => 'btn btn-mini btn-danger' %>

そうでない場合は、 current_url を flash[:fromurl] に保存してから、次のように Controller destroy セクションに配置するのが理にかなっていますか?

   respond_to do |format|
     format.html { redirect_to flash[:fromurl] }
     format.json { head :no_content }
   end

助けてくれてありがとう!

4

3 に答える 3

27

以下を使用できますredirect_to :back

respond_to do |format|
  format.html { redirect_to :back }
  format.json { head :no_content }
end

リクエストのヘッダー「HTTP_REFERER」を使用します。

redirect_to :back
# is a shorthand for:
redirect_to request.env["HTTP_REFERER"]
于 2013-05-01T19:06:16.647 に答える
4

Rails 5(公式ドキュメント)では、更新されたものを使用できます: redirect_back(fallback_location: root_path)

これを使用すると、ユーザーは参照ページ (前のページ、 と同じredirect_to :back) にリダイレクトされます。これが不可能な場合は、コントローラーで指定されたフォールバックの場所にリダイレクトします。

コントローラーのメソッドの例 (これにより、ユーザーはフォールバックの場所としてルート パスにリダイレクトされます):

def some_method
    #Your Code Here
    redirect_back(fallback_location: root_path)
end

expenseを削除してリダイレクトし、 にフォールバックするメソッドの例root_path:

def destroy
    @expense.destroy
    redirect_back(fallback_location: root_path)
end

fallback_location以下は、参照ページをリダイレクトできない場合に、ブラウザを特定のページにリダイレクトするために使用できる の他の例です。

redirect_back fallback_location: "http://www.google.com"      #Redirects to Google (or any website you specify)
redirect_back fallback_location:  "/images/screenshot.jpg"    #Redirects to a local image
redirect_back fallback_location:  posts_path                  #Redirects to the 'posts' path
redirect_back fallback_location:  new_user_path               #Redirects to the new user path 
于 2016-10-17T00:35:48.677 に答える