gpt4 book ai didi

html - View 在 redirect_to 后丢失 CSS 链接标记

转载 作者:行者123 更新时间:2023-11-28 10:48:53 26 4
gpt4 key购买 nike

我目前正在开发一个网络应用程序,但遇到了一个小问题。我的 View Controller 中有一个 redirect_to root_path 语句,如果逻辑语句返回 true,则调用该语句。在被重定向到的页面上,我在布局 View 中有一个 stylesheet_link_tag。当我手动拉起页面时,这个标签加载正确。但是,在使用 redirect_to 语句并从中加载此页面后,样式表标签不再存在。它仍然加载 application.css 文件和我的 application.js 文件没有任何问题,但 pagename.css 链接标记甚至没有显示。

页面显示如下:

http://i.stack.imgur.com/tu25b.png

然后一旦我刷新:

http://i.stack.imgur.com/hDtzk.png

请帮帮我!我很想知道为什么会发生这种情况以及如何解决它。提前致谢!

这里是所有相关的代码位:

用户 Controller .rb

...

def logout
cookies[:logged_in] = false
cookies[:user_id] = 0
redirect_to root_path # Tried using root_url also; didn't fix the problem.
end

...

主页/index.html.erb (root_path)

A bunch of HTML. Not the problem.

layouts/homepage.html.erb

<!DOCTYPE html>
<html>
<head>
<title>...</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag 'homepage' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>

<body>
<div id="header_container">
<%= link_to "#{image_tag 'logo.png'}".html_safe, root_path %>
<div class="login tab">
<span>
Login
<%= image_tag "down_arrow.png" %>
</span>

<%= form_tag login_path, :method => 'post' do %>
<% end %>

</div>
<div class="register tab">
<span>
<%= link_to "Register", register_path, "data-ajax" => "false", :rel => false %>
</span>
</div>
</div>

<div id="main_container">
<%= yield %>
</div>
</body>
</html>

我必须将 homepage.css Assets 添加到 config/locales/application.rb,所以如下:

require File.expand_path('../boot', __FILE__)

require 'rails/all'

Bundler.require(*Rails.groups)

module Labs
class Application < Rails::Application

config.assets.precompile += %w( homepage.css )
config.assets.precompile += %w( dropdown.js )
end
end

最佳答案

问题可能有两个方面:

--

涡轮链接

首先,您可能Turbolinks 有疑问

Turbolinks 因阻止页面样式和 javascript 更改而臭名昭著;因为它基本上只是刷新 <body>您页面的标签(保持 <head> 完好无损)。虽然这主要适用于您是否通过链接点击遍历页面(我不确定 redirects ),但您基本上需要确保即使启用了 Turbolinks 也可以发生任何样式更改。

为此,您需要确保加载时没有调用 Turbolinks。为此,您应该 use the data no-turbolink tag ,如下:

<%= link_to "link", link_path, data: { no_turbolink: true } %>

这将确保您不会调用 turbolinks当您单击该特定链接时,如果您愿意,请确保“裸”刷新。

--

重定向

重定向方法可能是您遇到的问题的原因。

当你提到:

The redirect_to statement is used and this page is loaded from it, the stylesheet tag is no longer there. It still loads the application.css file and my application.js file without anyone issues but the pagename.css link tag doesn't even show up.

解决此问题的方法是确保您的 layout包括正确的 stylesheets .你已经包含了你的 homepage.html.erb代码 - 我很想说如果 pagename.css没有加载,这基本上意味着您没有正确调用文件。

我会这样做:

<%= stylesheet_link_tag controller_name %>

这将允许您调用每个 Controller 的样式表(使用 controller_name helper method )——允许您更广泛地控制您在页面上加载的内容

关于html - View 在 redirect_to 后丢失 CSS 链接标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24048602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com