如何在 Ruby on Rails 中重新加载当前页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7465259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I reload the current page in Ruby on Rails?
提问by EverTheLearner
I currently have a login popup in my header bar which is on every page in my website. I want to be able to reload the current page that the person is on after a successful login. How do I do this in the controller?
我目前在我网站的每个页面上的标题栏中都有一个登录弹出窗口。我希望能够在成功登录后重新加载此人所在的当前页面。我如何在控制器中执行此操作?
def create
#declaring and defining user variable stuff
if user.save
#reload current page <--how do I do this?
end
end
Thanks
谢谢
回答by datalost
For my application, I use redirect_to :backand it does the trick. However, I doubt this might have an error in a non general use case(s) (user came from a special page?) but i haven't found it so far in my app.
对于我的应用程序,我使用redirect_to :back它来解决问题。但是,我怀疑这在非一般用例中可能有错误(用户来自特殊页面?)但到目前为止我还没有在我的应用程序中找到它。
回答by Archonic
If you're looking for a way to get the page to refresh (typically redirect_to :back) with an XHR request, you don't have to look for a way to change the response type - just tell the page to reload with inline JS.
如果您正在寻找一种redirect_to :back使用 XHR 请求刷新(通常)页面的方法,则不必寻找更改响应类型的方法 - 只需告诉页面使用内联 JS 重新加载即可。
format.js {render inline: "location.reload();" }
format.js {render inline: "location.reload();" }
Like Elenamentions, this should go in a respond_to block, like so:
就像Elena提到的那样,这应该放在 response_to 块中,如下所示:
respond_to do |format|
format.js {render inline: "location.reload();" }
end
回答by bishal
In Rails 5 redirect_to :backis improvedby:
在 Rails 5redirect_to :back中,改进了:
redirect_back(fallback_location: root_path)
回答by Elena G. Washington
Archonic's answer above worked for me. However, in Rails 3, I had to place this in a respond_toblock in order to avoid an 'ArgumentError (too few arguments)' error:
上面Archonic的回答对我有用。但是,在 Rails 3 中,我不得不将它放在一个respond_to块中,以避免出现“ArgumentError(参数太少)”错误:
respond_to do |format|
format.js {render inline: "location.reload();" }
end
回答by Shobhit
Since Rails 5 (or maybe older versions), you have a request.referrermethod. You simply redirect from controller to referrer and it opens the page where request came from.
从 Rails 5(或者可能是旧版本)开始,你有一个request.referrer方法。您只需从控制器重定向到引用者,它就会打开请求来自的页面。
redirect_to request.referrer, notice: "You're being redirected"
redirect_to request.referrer, notice: "You're being redirected"
回答by Kiryl Plyashkevich
Rails 5 introduced alternative function:
Rails 5 引入了替代功能:
redirect_back(fallback_location: root_path)
It redirect back whenever the HTTP_REFERER is known. Otherwise it redirects to the fallback_location.
只要 HTTP_REFERER 已知,它就会重定向回来。否则它会重定向到 fallback_location。
The redirect_to :backis deprecated in Rails 5.0 https://github.com/rails/rails/pull/22506and removed since Rails 5.1
的redirect_to :back是在5.0的Rails弃用https://github.com/rails/rails/pull/22506和去除,因为导轨5.1
回答by SMAG
Building on Archonic's and Elena's Answers, the reload function accepts a parameter to force the page to reload from the server aka forceGet, instead of from cache. A parameter can be set by the controller logic, like successful or failed login of a user, to trigger the desired behavior when it is sent to the page.
基于Archonic和Elena的答案,重新加载函数接受一个参数来强制页面从服务器(又名forceGet)重新加载,而不是从缓存中重新加载。控制器逻辑可以设置参数,例如用户登录成功或失败,以在将其发送到页面时触发所需的行为。
# force_get = controller_logic ? true : false
respond_to do |format|
format.js { render inline: "location.reload(#{force_get});" }
end

