有没有办法在link_to中包含重定向?我想在删除后刷新当前页面.但是,您可以从应用程序中的多个视图中删除相同的记录.
这是link_to:
<%= link_to 'Delete', expense, confirm: 'Are you sure?', method: :delete, :class => 'btn btn-mini btn-danger' %>
Run Code Online (Sandbox Code Playgroud)
如果没有,将current_url保存在flash [:fromurl]中然后将其放入Controller destroy部分是这样的:
respond_to do |format|
format.html { redirect_to flash[:fromurl] }
format.json { head :no_content }
end
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!
MrY*_*iji 24
你可以使用redirect_to :back:
respond_to do |format|
format.html { redirect_to :back }
format.json { head :no_content }
end
Run Code Online (Sandbox Code Playgroud)
它使用来自请求的标题"HTTP_REFERER":
redirect_to :back
# is a shorthand for:
redirect_to request.env["HTTP_REFERER"]
Run Code Online (Sandbox Code Playgroud)
在 Rails 5(官方文档)中,您可以使用更新的:
redirect_back(fallback_location: root_path)
使用此选项会将用户重定向到引用页面(上一页,与 相同redirect_to :back)。如果这不可能,则会重定向到控制器中指定的后备位置。
控制器中的方法示例(这会将用户重定向到根路径作为后备位置):
def some_method
#Your Code Here
redirect_back(fallback_location: root_path)
end
Run Code Online (Sandbox Code Playgroud)
删除并重定向回来的方法示例expense,并回退到root_path:
def destroy
@expense.destroy
redirect_back(fallback_location: root_path)
end
Run Code Online (Sandbox Code Playgroud)
以下是 的一些其他示例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
Run Code Online (Sandbox Code Playgroud)