身份验证前出现 Turbo Frame 错误“内容缺失”

问题描述 投票:0回答:1

我用 Turbo Frame 重建了这个 Cookie 同意教程,它有一些我无法理解的奇怪行为。

在主页和登录页面(即用户未登录的页面)上,即使我可以看到 DOM 中存在 Turbo-frame-tag (

<turbo-frame id="cookie_consent"
),也会呈现“内容缺失”。但是source属性设置为sign_in路由,为什么会这样呢?

<turbo-frame id="cookie_consent" src="http://localhost:3000/users/sign_in" complete="">
  <strong class="turbo-frame-error">Content missing</strong>
</turbo-frame>

控制台打印出这个错误:

Uncaught (in promise) Error: The response (200) did not contain the expected <turbo-frame id="cookie_consent"> and will be ignored. To perform a full page visit instead, set turbo-visit-control to reload.

相反,我希望显示 cookie 横幅。然后,当用户成功登录时,他们将被重定向到 /cookies 路由(索引操作),并且横幅会显示两次。这是怎么回事?

application.html.erb

<body>
    <%= render 'shared/flashes' %>
    <%= render 'shared/header' %>

    <%= yield %>
    <%= turbo_frame_tag :cookie_consent, src: cookies_path if session[:cookies_consent].nil? %>

    <%= render 'shared/footer' %>
  </body>

路线.rb

 get 'cookies', to: 'cookies#index'
 post 'cookies/consent', to: 'cookies#consent', as: 'cookie_consent'

cookies/index.html.erb

<%= turbo_frame_tag :cookie_consent do %>
    <% if session[:cookie_consent].nil? %>
        <div class="cookies" data-controller="cookies" data-cookies-target="banner">
            <p>We use essential cookies to make our site work. By clicking 'Ok', you agree to our website's cookie use as described in our <%= link_to 'Cookie Policy', privacy_path %>.
            <%= button_to 'OK',
                    cookie_consent_path(consent: true),
                    method: :post,
                    class: 'cookies__button',
                    data: { action: 'click->cookies#closeBanner' } %>
        </div>
    <% end %>
<% end %>

cookies_controller.rb

class CookiesController < ApplicationController
  def index
    session[:cookie_consent] = nil
  end

  def consent
    session[:cookie_consent] = params[:consent].presence
    render turbo_stream: turbo_stream.remove(:cookie_consent)
  end
end
ruby-on-rails ruby-on-rails-7 hotwire-rails turbo turbo-frames
1个回答
0
投票

你应该添加skip_before_action:authenticate_user!在你的 CookiesController 中。它帮助了我。

© www.soinside.com 2019 - 2024. All rights reserved.