簡體   English   中英

更新后如何返回上一頁?

[英]How can I go back to the previous page after updating?

單擊更新按鈕后,如何從編輯視圖(edit.html.erb)返回上一頁(列出有問題的項目)? 我努力了:

redirect_to request.referer #stays on the same page(edit.html.erb) 
redirect_to session.delete(:return_to) #outputs "Cannot redirect to nil!"
redirect_to :back #stays on same page
redirect_back fallback_location: posts_path #stays on same page
redirect_to(request.env['HTTP_REFERER']) #stays on same page

在數據庫中,注釋已更新。

雖然我可以使用posts_path(@comment),但我也想編輯另一個URL的注釋。 因此,需要重定向到上一頁。

您無法調用request.referer直接返回上一個編輯頁面。 由於update方法的原因, request.referer將始終是edit路徑。

您可以執行一些技巧,例如解析request.referer edit操作(可能是index ,例如作為update操作的參數,並重定向到更新資源的路徑)

# edit form view
<%= hidden_field_tag :previous_request, request.referer %>

# update action
def update
  ...
  redirect_to params[:previous_request]
end

暫無
暫無

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

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