简体   繁体   English

Rails 重定向 flash[:notice] 在不应该的情况下持续存在

[英]Rails redirect flash[:notice] persisting when not supposed to

I have a button with a redirect_to that runs my controller action, which creates a new row for my model.我有一个带有 redirect_to 的按钮,它运行我的 controller 操作,它为我的 model 创建了一个新行。 The first time, it creates it correctly and redirects.第一次,它正确创建并重定向。 After my redirect, I go to the previous page through the menu and repeat the same action.在我的重定向之后,我通过菜单 go 到上一页并重复同样的动作。 After I click on the button, it redirects to the correct page (which it shouldn't yet... caching?), and then my previous flash message displays.单击按钮后,它会重定向到正确的页面(它不应该...缓存?),然后显示我之前的 flash 消息。 So it's duplicated.所以它是重复的。 I put a debugger statement in to see where it happens on the second run through - it actually happens before my button action executes.我放入了一个调试器语句,以查看它在第二次运行时发生的位置——它实际上发生在我的按钮操作执行之前。 After running the rest of the code, it redirects (but since it redirected prematurely, redirects to same page) correctly with the corresponding (second) flash.运行代码的 rest 后,它使用相应的(第二个)flash 正确重定向(但由于它过早地重定向,重定向到同一页面)。 How do I get rid of the extra initial flash message?如何摆脱额外的初始 flash 消息?

Here's a picture showing what I mean:这是一张显示我的意思的图片:

在此处输入图像描述

If you look at the blue loading bar right underneath the URL, it shows that the page hasn't loaded yet (I stopped it with debugger statement as shown below).如果您查看 URL 正下方的蓝色加载栏,则表明该页面尚未加载(我使用调试器语句将其停止,如下所示)。 However, the redirect and flash has already happened, which it isn't supposed to, since the intended redirect and flash will happen after turbolinks finishes the page load.但是,重定向和 flash 已经发生,这是不应该的,因为预期的重定向和 flash 将在 turbolinks 完成页面加载后发生。

Initial link:初始链接:

 <%= link_to create_wager_from_favorite_wager_path(favorite_wager), data: { confirm: 'Create this wager?' } do %> Create Wager <% end %>

Controller action: Controller 动作:

 def create_wager_from debugger # on second run through, the redirect and flash happens before I reach this point @favorite_wager = FavoriteWager.find(params[:id]) @anchor = params[:anchor] set_member_statuses() result_message = @favorite_wager.create_wager_from_favorite(current_user) respond_to do |format| format.html { redirect_to my_wagers_path(:game => @favorite_wager.game), notice: "Wager created successfully!" } end end

At this point, it follows the standard path and I'm 99% sure the rest of the code is irrelevant.在这一点上,它遵循标准路径,我 99% 确定代码的 rest 是无关紧要的。

I've tried checking the flash params at the point that the page with the button action loads, but it's empty.我尝试在加载按钮操作的页面时检查 flash 参数,但它是空的。 So I'm not sure what's causing this issue.所以我不确定是什么导致了这个问题。 Any insight appreciated.任何见解表示赞赏。

Update: changing to flash.now[:notice] makes the duplicates stop, but the flash only displays on the first click of the button.更新:更改为 flash.now[:notice] 使重复停止,但 flash 仅在第一次单击按钮时显示。 Then it doesn't appear anytime after.然后它不会在任何时候出现。 And refreshing the page will allow the error to be repeated.刷新页面将允许错误重复。

After reading up on Turbolinks, I'd determined that the cause of this issue is the natural built-in functionality in turbolinks called a "page preview".在阅读了 Turbolinks 之后,我确定这个问题的原因是 turbolinks 中称为“页面预览”的自然内置功能。 This is where it will display the previous cached page as a sort of "preview" before the server response arrives, which gives the illusion that the page already loaded.这是它将在服务器响应到达之前将先前缓存的页面显示为一种“预览”的地方,这给人一种页面已经加载的错觉。

However, the cached redirect page in my case was cached the moment it was served up, meaning the flash message was also captured into that cache.但是,在我的案例中,缓存的重定向页面在提供时就被缓存了,这意味着 flash 消息也被捕获到该缓存中。 So on the second time I clicked the create button, it would load the cached page with the flash, then redirect for real and flash again (like it's suppoed to).因此,在我第二次单击创建按钮时,它会使用 flash 加载缓存页面,然后再次重定向为真实页面和 flash(就像它应该做的那样)。

So the solution here is to either a.所以这里的解决方案是要么 a. disable all page previews or b.禁用所有页面预览或 b。 disable turbolinks for that specific link.禁用该特定链接的 turbolinks。 I chose b.我选择了b。 because it won't affect the rest of my program, at the expense that the blue loading motion is not longer there.因为它不会影响我的程序的 rest,代价是蓝色加载动作不再存在。 Here is the solution below (very simple):这是下面的解决方案(非常简单):

Before:前:

 <%= link_to create_wager_from_favorite_wager_path(fw, :anchor => anchor), data: { confirm: 'Create this wager?' }, class: "red-btn create-favorite-wager-btn" do %> Create Wager <% end %>

After:后:

 <%= link_to create_wager_from_favorite_wager_path(fw, :anchor => anchor), data: { confirm: 'Create this wager?' }, "data-turbolinks": "false", class: "red-btn create-favorite-wager-btn" do %> Create Wager <% end %>

To prevent elements from being visible in cached pages (including previews), remove the element on turbolinks:before-cache .为了防止元素在缓存页面(包括预览)中可见,请删除turbolinks:before-cache上的元素。 For example you could include something like this in your main application JavaScript file:例如,您可以在主应用程序 JavaScript 文件中包含类似的内容:

addEventListener('turbolinks:before-cache', () => {
  document.querySelectorAll('.flash').forEach(element => element.remove())
})

For more on this checkout https://github.com/turbolinks/turbolinks#understanding-caching有关此结帐的更多信息https://github.com/turbolinks/turbolinks#understanding-caching

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

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