简体   繁体   中英

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

How can I go back to the previous page (where the items in question were listed) from the edit view (edit.html.erb) after clicking on the update button? I have tried:

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

In the database, the comment is updated.

While I can use posts_path(@comment), I want to also edit comment from another url. Hence the need for a redirect to the previous page.

You can't call request.referer to directly back the the previous of edit page. Cause on the update method, the request.referer will be always the edit path.

You can, do a trick like, parse the request.referer of edit action (might be the index one, for ex. as params to update action, and redirect to the path on updating the resource )

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM