删除后Rails重定向到上一页(后面)

时间:2022-06-01 17:49:24

Is there a way to include a redirect in a link_to? I want to just refresh the current page after a delete. But, you can delete the same record from multiple views in the app.

有没有办法在link_to中包含重定向?我想在删除后刷新当前页面。但是,您可以从应用程序中的多个视图中删除相同的记录。

This is the link_to:

这是link_to:

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

If not, does it make sense to save the current_url in flash[:fromurl] then put that in the Controller destroy section like this:

如果没有,将current_url保存在flash [:fromurl]中然后将它放在Controller destroy部分中是否有意义:

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

Thanks for the help!

谢谢您的帮助!

3 个解决方案

#1


24  

You can use the redirect_to :back :

你可以使用redirect_to:back:

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

It uses the header "HTTP_REFERER" from the request:

它使用来自请求的标题“HTTP_REFERER”:

redirect_to :back
# is a shorthand for:
redirect_to request.env["HTTP_REFERER"]

#2


2  

In Rails 5 (Official Docs) you can use the updated: redirect_back(fallback_location: root_path)

在Rails 5(官方文档)中,您可以使用更新的:redirect_back(fallback_location:root_path)

Using this redirects the user to the referring page (previous page, the same as redirect_to :back). If this isn't possible, it then redirects to a fallback location specified in the controller.

使用此功能可将用户重定向到引用页面(上一页,与redirect_to:back相同)。如果无法做到这一点,则会重定向到控制器中指定的回退位置。

An example of a method in your controller (this redirects the user to the root path as a fallback location):

控制器中的方法示例(这会将用户重定向到根路径作为后备位置):

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

An example of a method for deleting an expense and redirecting back, with a fallback to the root_path:

删除费用并重定向的方法示例,其中包含回退到root_path:

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

Below are some other examples of the fallback_location's that can be used to redirect the browser back to a specific page, if the referring page cannot be redirected to:

以下是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 

#3


1  

You can also try this one.

你也可以试试这个。

render :nothing => true

#1


24  

You can use the redirect_to :back :

你可以使用redirect_to:back:

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

It uses the header "HTTP_REFERER" from the request:

它使用来自请求的标题“HTTP_REFERER”:

redirect_to :back
# is a shorthand for:
redirect_to request.env["HTTP_REFERER"]

#2


2  

In Rails 5 (Official Docs) you can use the updated: redirect_back(fallback_location: root_path)

在Rails 5(官方文档)中,您可以使用更新的:redirect_back(fallback_location:root_path)

Using this redirects the user to the referring page (previous page, the same as redirect_to :back). If this isn't possible, it then redirects to a fallback location specified in the controller.

使用此功能可将用户重定向到引用页面(上一页,与redirect_to:back相同)。如果无法做到这一点,则会重定向到控制器中指定的回退位置。

An example of a method in your controller (this redirects the user to the root path as a fallback location):

控制器中的方法示例(这会将用户重定向到根路径作为后备位置):

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

An example of a method for deleting an expense and redirecting back, with a fallback to the root_path:

删除费用并重定向的方法示例,其中包含回退到root_path:

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

Below are some other examples of the fallback_location's that can be used to redirect the browser back to a specific page, if the referring page cannot be redirected to:

以下是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 

#3


1  

You can also try this one.

你也可以试试这个。

render :nothing => true