Ruby on rails application.html.erb仍然没有呈现
我生成了一个Rails应用程序,正在处理其内部。以前我的application.html.erb是正确渲染的,但现在Rails似乎完全忽略了它,因为它甚至不会生成错误 关于这个问题,有很多关于堆栈溢出的问题。我已经看过了我认为是所有这些,但没有一个有帮助 我的路线:Ruby on rails application.html.erb仍然没有呈现,ruby-on-rails,ruby,views,Ruby On Rails,Ruby,Views,我生成了一个Rails应用程序,正在处理其内部。以前我的application.html.erb是正确渲染的,但现在Rails似乎完全忽略了它,因为它甚至不会生成错误 关于这个问题,有很多关于堆栈溢出的问题。我已经看过了我认为是所有这些,但没有一个有帮助 我的路线: Rails.application.routes.draw do # static_pages from rails tutorial ch. 3 get 'static_pages/home' get 'static
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
以下是视图/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>
我试图在application.html.erb中有目的地生成一个错误,调用不存在的变量和任何其他诡计。Rails完全无视我,我感到不安全
我想做的就是在
中显示页面名称,但我甚至无法获得正确呈现的纯文本。如何使其工作,以便在标题中正确地获取控制器变量时失败?您不应覆盖控制器初始化
方法。这样做将破坏基类行为
我相信,只要从初始化调用super
就可以解决您的问题,但Rails为特定操作初始化控制器的正确方法是使用
例如:
class StaticPagesController < ApplicationController
layout 'application'
before_action :load_locals
def load_locals
@locals = {:page_title => 'Default'}
end
...
end
class StaticPagesController'Default'}
终止
...
终止
是的,你明白了。我可以问一下我应该如何创建@locals来传递给视图吗?尝试使用before\u action
过滤器,或者从所有操作中调用一些常用方法。
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
|-layouts
||-application.html.erb
|
|-static_pages
||-about.html.erb
||-home.html.erb
||-help.html.erb
class StaticPagesController < ApplicationController
layout 'application'
before_action :load_locals
def load_locals
@locals = {:page_title => 'Default'}
end
...
end