简体   繁体   English

如何在 Ruby on Rails 中重新加载当前页面?

[英]How can I reload the current page in Ruby on Rails?

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谢谢

For my application, I use redirect_to :back and it does the trick.对于我的应用程序,我使用redirect_to :back并且它可以解决问题。 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.但是,我怀疑这在非一般用例中可能有错误(用户来自特殊页面?)但到目前为止我还没有在我的应用程序中找到它。

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.如果您正在寻找一种使用 XHR 请求刷新页面的方法(通常是redirect_to :back ),则不必寻找更改响应类型的方法 - 只需告诉页面使用内联 JS 重新加载.

format.js {render inline: "location.reload();" }

Like Elena mentions, this should go in a respond_to block, like so:就像Elena提到的那样,这应该放在 response_to 块中,如下所示:

respond_to do |format|
  format.js {render inline: "location.reload();" }
end

在 Rails 5 中, redirect_to :back 改进了:

    redirect_back(fallback_location: root_path)

Archonic 's answer above worked for me.上面Archonic的回答对我有用 However, in Rails 3, I had to place this in a respond_to block in order to avoid an 'ArgumentError (too few arguments)' error:但是,在 Rails 3 中,我不得不将它放在respond_to块中,以避免出现“ArgumentError(参数太少)”错误:

respond_to do |format|
  format.js {render inline: "location.reload();" }
end

Since Rails 5 (or maybe older versions), you have a request.referrer method.从 Rails 5(或者可能是旧版本)开始,您有一个request.referrer方法。 You simply redirect from controller to referrer and it opens the page where request came from.您只需从控制器重定向到引用者,它就会打开请求来自的页面。

redirect_to request.referrer, notice: "You're being redirected"

Rails 5 introduced alternative function: Rails 5 引入了替代功能:

redirect_back(fallback_location: root_path)

It redirect back whenever the HTTP_REFERER is known.只要 HTTP_REFERER 已知,它就会重定向回来。 Otherwise it redirects to the fallback_location.否则它会重定向到 fallback_location。

The redirect_to :back is deprecated in Rails 5.0 https://github.com/rails/rails/pull/22506 and removed since Rails 5.1 redirect_to :back在 Rails 5.0 https://github.com/rails/rails/pull/22506 中已弃用,并自 Rails 5.1 起删除

This syntax is what you want... works in Rails 6这个语法就是你想要的......适用于 Rails 6

  respond_to do |format|
    format.html { redirect_to request.referrer, notice: "User was successfully WHATEVER." }
  end

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.基于ArchonicElena的答案,重新加载函数接受一个参数来强制页面从服务器(又名forceGet)重新加载,而不是从缓存中重新加载。 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.控制器逻辑可以设置参数,例如用户登录成功或失败,以在将其发送到页面时触发所需的行为。

# force_get = controller_logic ? true : false

respond_to do |format|
  format.js { render inline: "location.reload(#{force_get});" }
end

UPDATE: the logic has been deprecated for the forceGet option.更新: forceGet 选项的逻辑已被弃用。 If you do want to reload from the server you can use this logic:如果您确实想从服务器重新加载,您可以使用以下逻辑:

# force_get = controller_logic ? true : false

respond_to do |format|
  format.js { render inline: force_get ? "window.location.href = window.location.href;" : "location.reload();" }
end

just redirect to whatever url you want in the function:只需重定向到您想要的函数中的任何网址:

redirect_to what_ever_path redirect_to what_ever_path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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