我想在登陆页面上显示我的登录部分。我在Rails 7中构建了一个2布局站点,视图/布局/application.html.erb和视图/ layout /landing.html.erb。每一个都包含一个收益。
当未登录时,我将正确地转到登陆页。
我当前的问题是,当我单击“登录”按钮时,devise /devise/sessions/new.html.erb不会呈现。我加了skip_before_action :authenticate_user!在我的StaticPagesController中,以确保devise接受这是可以查看的,而无需登录。如果我删除skip_before_action :authenticate_user!在我的StaticPagesController中,它每次都呈现视图/devise/sessions/new.html.erb,而不是放置在视图/static/landing.html.erb中的登陆页文本欢迎文本。
我的服务器使用skip_before_action :authenticate_user登录!在StaticPagesController和按钮中单击:
hellorails-web-1 | Started POST "/users" for 172.22.0.1 at 2022-11-10 09:23:44 +0000
hellorails-web-1 | Processing by Devise::RegistrationsController#create as TURBO_STREAM
hellorails-web-1 | Parameters: {"authenticity_token"=>"[FILTERED]"}
hellorails-web-1 | Rendering layout layouts/landing.html.erb
hellorails-web-1 | Rendering devise/registrations/new.html.erb within layouts/landing
hellorails-web-1 | Rendered devise/shared/_error_messages.html.erb (Duration: 1.9ms | Allocations: 942)
hellorails-web-1 | Rendered devise/shared/_links.html.erb (Duration: 0.3ms | Allocations: 136)
hellorails-web-1 | Rendered devise/registrations/new.html.erb within layouts/landing (Duration: 6.6ms | Allocations: 3479)它试图在正确的布局中呈现正确的数据,但它没有在我的浏览器中反映这一点。
我的服务器使用skip_before_action :authenticate_user登录!在StaticPagesController中删除。正如预期的那样,401,然后在我的模板中呈现登录页面。
hellorails-web-1 | Started GET "/" for 172.22.0.1 at 2022-11-10 09:31:27 +0000
hellorails-web-1 | Processing by StaticPagesController#landing as HTML
hellorails-web-1 | Completed 401 Unauthorized in 2ms (Allocations: 1041)
hellorails-web-1 |
hellorails-web-1 |
hellorails-web-1 | Started GET "/users/sign_in" for 172.22.0.1 at 2022-11-10 09:31:27 +0000
hellorails-web-1 | Processing by Devise::SessionsController#new as HTML
hellorails-web-1 | Rendering layout layouts/landing.html.erb
hellorails-web-1 | Rendering devise/sessions/new.html.erb within layouts/landing
hellorails-web-1 | Rendered devise/shared/_links.html.erb (Duration: 0.3ms | Allocations: 210)
hellorails-web-1 | Rendered devise/sessions/new.html.erb within layouts/landing (Duration: 2.5ms | Allocations: 1538)
hellorails-web-1 | Rendered layout layouts/landing.html.erb (Duration: 11.3ms | Allocations: 3945)
hellorails-web-1 | Completed 200 OK in 48ms (Views: 12.6ms | ActiveRecord: 12.2ms | Allocations: 17279)这可能是一个涡轮流问题,因为这是我在响应类型中看到的唯一不同之处,但我似乎无法找到解决这个问题的方法。
这是我的设置:
我的静态页面控制器:
class StaticPagesController < ApplicationController
skip_before_action :authenticate_user! #If this is removed devise login page is rendered every time
def landing
layout "landing"
end
end我的应用程序控制器:
class ApplicationController < ActionController::Base
before_action :authenticate_user!
layout :layout_by_resource
private
def layout_by_resource
if devise_controller? #Add later: skip landing if logged in.
"landing"
else
"application"
end
end
end我的路线:
Rails.application.routes.draw do
get 'static_pages/landing'
devise_for :users
root 'static_pages#landing'
end剥离的landing.html.erb布局:
<!DOCTYPE html>
<html class="h-full">
<head>
<title>Landing</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
<link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700" rel="stylesheet">
</head>
<body class="leading-normal tracking-normal text-gray-900" style="font-family: 'Source Sans Pro', sans-serif;">
<div class="h-screen pb-14 bg-right bg-cover" style="background-image:url(<%=asset_path('bg.svg')%>);">
<% if user_signed_in? %>
<%= button_to("Sign out",
user_registration_path,
class:"bg-blue-500 hover:bg-indigo-800 text-white font-bold py-2 px-4 rounded",
) %>
<% else %>
<%= button_to("Sign in", user_registration_path, class:"bg-blue-500 hover:bg-indigo-800 text-white font-bold py-2 px-4 rounded") %>
<% end %>
</div>
<%= yield %>
</body>
</html>添加到turbo流的设备配置中:
config.navigational_formats = ['*/*', :html, :turbo_stream]提前感谢
发布于 2022-11-10 10:57:59
一个选项是禁用button_to的turbo,例如:
<%= button_to("Sign in", user_registration_path, data: { turbo: false }, class:"bg-blue-500 hover:bg-indigo-800 text-white font-bold py-2 px-4 rounded") %>https://stackoverflow.com/questions/74386986
复制相似问题