簡體   English   中英

刪除后Rails重定向到上一頁(后面)

[英]Rails redirect to previous page (back) after delete

有沒有辦法在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

謝謝您的幫助!

你可以使用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"]

在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 

你也可以試試這個。

render :nothing => true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM