application.html.erb 仍未呈现 [英] application.html.erb is still not rendering
问题描述
我生成了一个 Rails 应用程序,并且正在研究内部结构.以前我的 application.html.erb 可以正确呈现,但现在 Rails 似乎完全忽略了它,因为它甚至不会产生错误.
I generated a Rails application, and am playing around with the internals. Previously my application.html.erb was rendering properly, but now it seems like Rails is totally ignoring it because it won't even generate an error.
在 Stack Overflow 上有很多关于这个问题的问题.我已经查看了我认为的所有内容,但没有一个有帮助.
There have been a bunch of questions on Stack Overflow regarding this problem. I've looked at what I think is all of them, but none have helped.
我的路线:
Rails.application.routes.draw do
# static_pages from rails tutorial ch. 3
get 'static_pages/home'
get 'static_pages/help'
get 'static_pages/about'
end
这是views/layout/application.html.erb
Here is the views/layout/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>This Title is not showing up</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<p> why isnt this showing up?? </p>
<%= yield %>
</body>
</html>
这是 static_pages_controller:
Here is the static_pages_controller:
class StaticPagesController < ApplicationController
layout 'application' #<- I know this shouldn't be necessary, but I thought i'd try
def initialize
@locals = {:page_title => 'Default'}
end
def about
@locals[:page_title] = 'About'
render @locals
end
def help
@locals[:page_title] = 'Help'
render @locals
end
def home
@locals[:page_title] = 'Home'
render @locals
end
end
这是应用程序控制器:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
end
没有其他布局.我的 Views 文件夹具有以下结构:
There are no other layouts. My Views folder has the following structure:
-Views
|-layouts
||-application.html.erb
|
|-static_pages
||-about.html.erb
||-home.html.erb
||-help.html.erb
我尝试在 application.html.erb 中故意生成错误,调用不存在的变量以及任何其他恶作剧.Rails 完全无视我,我感到不安全.
I've tried purposefully generating an error in the application.html.erb, calling variables that don't exist and whatever other shenanigans. Rails is totally ignoring me and I'm feeling insecure.
我想要做的就是在
中显示页面名称,但我什至无法让纯文本正确呈现.我怎样才能让它工作,这样我才能正确地在标题中获取控制器变量失败?
All I wanted to do is to display the page name in the <title>
, but I can't even get plaintext to render correctly. How can I get this to work so that I can properly fail at getting the controller variable in the title?
推荐答案
您不应覆盖控制器的 initialize
方法.这样做会破坏基类行为.
You should not override the controller initialize
method. Doing this will break the base class behavior.
虽然,我相信,只需从 initialize
调用 super
即可解决您的问题,但为特定操作初始化控制器的正确 Rails 方法是使用过滤前.
While, I believe, just calling the super
from the initialize
will fix your issue, the correct Rails way to initialize a controller for a specific action is to use a before filter instead.
示例:
class StaticPagesController < ApplicationController
layout 'application'
before_action :load_locals
def load_locals
@locals = {:page_title => 'Default'}
end
...
end
这篇关于application.html.erb 仍未呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!