- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在开发一个网络应用程序,但遇到了一个小问题。我的 View Controller 中有一个 redirect_to root_path 语句,如果逻辑语句返回 true,则调用该语句。在被重定向到的页面上,我在布局 View 中有一个 stylesheet_link_tag。当我手动拉起页面时,这个标签加载正确。但是,在使用 redirect_to 语句并从中加载此页面后,样式表标签不再存在。它仍然加载 application.css 文件和我的 application.js 文件没有任何问题,但 pagename.css 链接标记甚至没有显示。
页面显示如下:
然后一旦我刷新:
请帮帮我!我很想知道为什么会发生这种情况以及如何解决它。提前致谢!
这里是所有相关的代码位:
用户 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/
我有一个简单的模型和 Controller 。让我们以新西兰人为例: def index @kiwis = Kiwi.all end def destroy kiwi = Kiwi.f
我怀疑这两个命令之间是否存在差异,当我想在 Rails 中创建没有 View 的操作时,我一直使用 redirect_to之后没有任何返回,我从来没有遇到任何问题,但直到我忘记放置 redirect_
我的代码是这样的: def index @monkeys = Monkey.where(owner: current_user) end def new @monkey = Monkey.ne
我有一个 Controller ,可以在特定条件下在特定点重定向。当我将参数传递给我的 Controller 规范(使用最新的 RSpec)中的规范辅助方法以触发这些条件时,我得到一个 ActionV
我的应用程序中有很长的表单,所以我设置了一个 _new_form.html.erb这是在我的 new.html.erb 中呈现的.在用户更新此表单并通过一些基本验证后,我希望它们被重定向到 edit.
代码是什么 redirect_to @user 在 Controller 方法中使用时,是指在 Rails 中吗? 它究竟重定向到哪里?它是重定向到用户 Controller (如果是, Contro
我有一个 Rails 应用程序,它有一个在 iframe 中呈现的表单,从创建操作我想从 iframe 中 redirect_to 并重新呈现整个页面而不是在iframe. 我很好奇有什么想法可以实现
我正在尝试根据提交的表单上的参数重定向到一个位置。 如果 params[:route] = group ,我想重定向到 groups_path。 我尝试了以下方法来重定向,但很明显,groups_pa
def confirm_invite_new_tutor redirect_with_msg = false @game_school = GameSchool.find(params
我在使用 rails 时遇到了一点问题,我希望能够做这样的事情来避免多次重定向: def render_not_found not_found end private def not_foun
我是菜鸟 redirect_to users_url, notice: 'Succeed.' 然后我添加了一条消息,但它失败了: redirect_to users_url, notice: 'Su
我的付款表单中有一个隐藏的输入,如下所示: "> " /> 支付后我会这样使用它: header("location: " . $_GET['redirect_to']); 一切正
我正在学习 Rails 并且遇到了 Rspec 的问题。我对 Controller 进行了以下测试: describe PostsController, "creating a new post" d
我试图在资源创建后显示它。 路线.rb resources :eco_systems do member do get 'new' post 'create'
我见过的最奇怪的事情。 在 Controller 中,我尝试重定向以将用户发送到另一个网站: redirect_to "http://google.com" --> This works. 但是,如果
Turbolinks 在整个应用程序中运行。我需要在某个场景中在 create 方法中重新加载页面。 这会重定向到正确的页面,但不会重新加载页面: redirect_to convos_path 这会
我已经尝试查看其他答案,但我似乎无法弄清楚为什么我的重定向不起作用。 所以我在 Rails 3.1 中使用 Devise,我正在制作一个购物网站。除非已登录,否则不允许访问者将商品添加到购物车。这就是
在我的 rails 应用程序中,我有一个学生模型和一个 material_student。在学生登录后,我会将其重定向到一个页面,让他可以选择在两个“编辑” View 之间进行选择。第一个由stude
我在 The Rails 3 Way 一书中看到了一个例子,它说 redirect_to post 这是否因为 post 而有什么特殊含义? , 或者如果它只是一个示例和 post 的糟糕选择只是一个
试图弄清楚为什么我的 nil 检查在调用没有参数的方法或不产生记录的参数 ID 时失败。 @game = Game.where(:id => params[:id]).first if @game.n
我是一名优秀的程序员,十分优秀!