application.html.erb 仍未呈现 [英] application.html.erb is still not rendering

查看:30
本文介绍了application.html.erb 仍未呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成了一个 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.

我想要做的就是在 </code> 中显示页面名称,但我什至无法让纯文本正确呈现.我怎样才能让它工作,这样我才能正确地在标题中获取控制器变量失败?<em class="showen"></em></p> <p class="en">All I wanted to do is to display the page name in the <code><title></code>, 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?</p> <h3 class="best_ans mt-1">推荐答案</h3> <p class="cn">您不应覆盖控制器的 <code>initialize</code> 方法.这样做会破坏基类行为.<em class="showen"></em></p> <p class="en">You should not override the controller <code>initialize</code> method. Doing this will break the base class behavior.</p> <p class="cn">虽然,我相信,只需从 <code>initialize</code> 调用 <code>super</code> 即可解决您的问题,但为特定操作初始化控制器的正确 Rails 方法是使用<a href="http://guides.rubyonrails.org/action_controller_overview.html#filters" rel="nofollow">过滤前</a>.<em class="showen"></em></p> <p class="en">While, I believe, just calling the <code>super</code> from the <code>initialize</code> will fix your issue, the correct Rails way to initialize a controller for a specific action is to use a before filter instead.</p> <p class="cn">示例:</p> <pre><code><code>class StaticPagesController < ApplicationController layout 'application' before_action :load_locals def load_locals @locals = {:page_title => 'Default'} end ... end </code></code></pre> <p>这篇关于application.html.erb 仍未呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!</p> </div> <div class="arc-body-main-more"> <span onclick="unlockarc('2467264');">查看全文</span> </div> </div> <div> </div> <div class="wwads-cn wwads-horizontal" data-id="166" style="max-width:100%;border: 4px solid #666;"></div> </div> </article> <div id="arc-ad-2" class="mb-1"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5038752844014834" crossorigin="anonymous"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5038752844014834" data-ad-slot="3921941283"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="widget bgwhite radius-1 mb-1 shadow widget-rel"> <h5>相关文章</h5> <ul> <li> <a target="_blank" title="来自application.html.erb的Rails渲染实例变量" href="/2134143.html"> 来自application.html.erb的Rails渲染实例变量; </a> </li> <li> <a target="_blank" title="在 Rails 应用程序中绕过 application.html.erb" href="/2386289.html"> 在 Rails 应用程序中绕过 application.html.erb; </a> </li> <li> <a target="_blank" title="javascript_include_tag 在 application.html.erb 中的 Rails 3.1 问题" href="/2385623.html"> javascript_include_tag 在 application.html.erb 中的 Rails 3.1 问题; </a> </li> <li> <a target="_blank" title="如何在Rails中使用erb标签呈现HTML内容?" href="/2066619.html"> 如何在Rails中使用erb标签呈现HTML内容?; </a> </li> <li> <a target="_blank" title="是否有可能加载+呈现另一个html.erb文件中的html.erb文件?" href="/879464.html"> 是否有可能加载+呈现另一个html.erb文件中的html.erb文件?; </a> </li> <li> <a target="_blank" title="Rails的'.js.erb'不呈现部分" href="/1490018.html"> Rails的'.js.erb'不呈现部分; </a> </li> <li> <a target="_blank" title="通过js.erb文件呈现Flash消息" href="/2084976.html"> 通过js.erb文件呈现Flash消息; </a> </li> <li> <a target="_blank" title="Rails 2.3.2尝试呈现ERB而不是HAML" href="/2116343.html"> Rails 2.3.2尝试呈现ERB而不是HAML; </a> </li> <li> <a target="_blank" title="将html.slim视图转换为html.erb-Rails,Slim,ERB" href="/2066584.html"> 将html.slim视图转换为html.erb-Rails,Slim,ERB; </a> </li> <li> <a target="_blank" title="如何返回js.html.erb文件中的index.html.erb" href="/2387620.html"> 如何返回js.html.erb文件中的index.html.erb; </a> </li> <li> <a target="_blank" title=".erb,.rhtml和.html.erb有什么区别?" href="/2066574.html"> .erb,.rhtml和.html.erb有什么区别?; </a> </li> <li> <a target="_blank" title="用ERB生成HTML文件?" href="/870897.html"> 用ERB生成HTML文件?; </a> </li> <li> <a target="_blank" title="h:尽管逸出="false",但输出文本仍未从XML响应中呈现HTML." href="/1501850.html"> h:尽管逸出="false",但输出文本仍未从XML响应中呈现HTML.; </a> </li> <li> <a target="_blank" title="为什么Rails无法呈现.js.erb文件?" href="/1486613.html"> 为什么Rails无法呈现.js.erb文件?; </a> </li> <li> <a target="_blank" title="Rails 3:如何在rake任务中呈现ERb模板?" href="/2066556.html"> Rails 3:如何在rake任务中呈现ERb模板?; </a> </li> <li> <a target="_blank" title="如何将字符串呈现为erb文件?" href="/2066561.html"> 如何将字符串呈现为erb文件?; </a> </li> <li> <a target="_blank" title="在erb模板中禁用HTML转义" href="/689748.html"> 在erb模板中禁用HTML转义; </a> </li> <li> <a target="_blank" title="如何将erb转换为html?" href="/2066612.html"> 如何将erb转换为html?; </a> </li> <li> <a target="_blank" title="在 erb 模板中禁用 HTML 转义" href="/2662493.html"> 在 erb 模板中禁用 HTML 转义; </a> </li> <li> <a target="_blank" title="如何使用erb使YAML文件在中间人中呈现" href="/2066592.html"> 如何使用erb使YAML文件在中间人中呈现; </a> </li> <li> <a target="_blank" title="Rails3呈现与文本/ html内容类型,而不是文本/ javascript中js.erb模板" href="/52634.html"> Rails3呈现与文本/ html内容类型,而不是文本/ javascript中js.erb模板; </a> </li> <li> <a target="_blank" title="什么呈现HTML?" href="/872274.html"> 什么呈现HTML?; </a> </li> <li> <a target="_blank" title="EJS呈现HTML" href="/2061155.html"> EJS呈现HTML; </a> </li> <li> <a target="_blank" title="如何将 erb 模板呈现为字符串内部动作?" href="/2385471.html"> 如何将 erb 模板呈现为字符串内部动作?; </a> </li> <li> <a target="_blank" title="为什么此方法不呈现我的.js.erb文件?" href="/1837289.html"> 为什么此方法不呈现我的.js.erb文件?; </a> </li> </ul> </div> <div class="mb-1"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-5038752844014834" crossorigin="anonymous"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-5038752844014834" data-ad-slot="3921941283"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> <div class="side"> <div class="widget widget-side bgwhite mb-1 shadow"> <h5>其他开发最新文章</h5> <ul> <li> <a target="_blank" title="拒绝显示一个框架,因为它将'X-Frame-Options'设置为'sameorigin'" href="/893060.html"> 拒绝显示一个框架,因为它将'X-Frame-Options'设置为'sameorigin'; </a> </li> <li> <a target="_blank" title="什么是&QUOT; AW&QUOT;在部分标志属性是什么意思?" href="/303988.html"> 什么是&QUOT; AW&QUOT;在部分标志属性是什么意思?; </a> </li> <li> <a target="_blank" title="在运行npm install命令时获取'npm WARN弃用'警告" href="/840917.html"> 在运行npm install命令时获取'npm WARN弃用'警告; </a> </li> <li> <a target="_blank" title="cmake无法找到openssl" href="/516280.html"> cmake无法找到openssl; </a> </li> <li> <a target="_blank" title="从Spark的scala中的* .tar.gz压缩文件中读取HDF5文件" href="/850628.html"> 从Spark的scala中的* .tar.gz压缩文件中读取HDF5文件; </a> </li> <li> <a target="_blank" title="Twitter :: Error :: Forbidden - 无法验证您的凭据" href="/630061.html"> Twitter :: Error :: Forbidden - 无法验证您的凭据; </a> </li> <li> <a target="_blank" title="我什么时候需要一个fb:app_id或者fb:admins?" href="/747981.html"> 我什么时候需要一个fb:app_id或者fb:admins?; </a> </li> <li> <a target="_blank" title="将.db文件导入R" href="/902960.html"> 将.db文件导入R; </a> </li> <li> <a target="_blank" title="npm通知创建一个lockfile作为package-lock.json。你应该提交这个文件" href="/744854.html"> npm通知创建一个lockfile作为package-lock.json。你应该提交这个文件; </a> </li> <li> <a target="_blank" title="拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src'self'”" href="/819167.html"> 拒绝执行内联脚本,因为它违反了以下内容安全策略指令:“script-src'self'”; </a> </li> </ul> </div> <div class="widget widget-side bgwhite mb-1 shadow"> <h5> 热门教程 </h5> <ul> <li> <a target="_blank" title="Java教程" href="/OnLineTutorial/java/index.html"> Java教程 </a> </li> <li> <a target="_blank" title="Apache ANT 教程" href="/OnLineTutorial/ant/index.html"> Apache ANT 教程 </a> </li> <li> <a target="_blank" title="Kali Linux教程" href="/OnLineTutorial/kali_linux/index.html"> Kali Linux教程 </a> </li> <li> <a target="_blank" title="JavaScript教程" href="/OnLineTutorial/javascript/index.html"> JavaScript教程 </a> </li> <li> <a target="_blank" title="JavaFx教程" href="/OnLineTutorial/javafx/index.html"> JavaFx教程 </a> </li> <li> <a target="_blank" title="MFC 教程" href="/OnLineTutorial/mfc/index.html"> MFC 教程 </a> </li> <li> <a target="_blank" title="Apache HTTP客户端教程" href="/OnLineTutorial/apache_httpclient/index.html"> Apache HTTP客户端教程 </a> </li> <li> <a target="_blank" title="Microsoft Visio 教程" href="/OnLineTutorial/microsoft_visio/index.html"> Microsoft Visio 教程 </a> </li> </ul> </div> <div class="widget widget-side bgwhite mb-1 shadow"> <h5> 热门工具 </h5> <ul> <li> <a target="_blank" title="Java 在线工具" href="/Onlinetools/details/4"> Java 在线工具 </a> </li> <li> <a target="_blank" title="C(GCC) 在线工具" href="/Onlinetools/details/6"> C(GCC) 在线工具 </a> </li> <li> <a target="_blank" title="PHP 在线工具" href="/Onlinetools/details/8"> PHP 在线工具 </a> </li> <li> <a target="_blank" title="C# 在线工具" href="/Onlinetools/details/1"> C# 在线工具 </a> </li> <li> <a target="_blank" title="Python 在线工具" href="/Onlinetools/details/5"> Python 在线工具 </a> </li> <li> <a target="_blank" title="MySQL 在线工具" href="/Onlinetools/Dbdetails/33"> MySQL 在线工具 </a> </li> <li> <a target="_blank" title="VB.NET 在线工具" href="/Onlinetools/details/2"> VB.NET 在线工具 </a> </li> <li> <a target="_blank" title="Lua 在线工具" href="/Onlinetools/details/14"> Lua 在线工具 </a> </li> <li> <a target="_blank" title="Oracle 在线工具" href="/Onlinetools/Dbdetails/35"> Oracle 在线工具 </a> </li> <li> <a target="_blank" title="C++(GCC) 在线工具" href="/Onlinetools/details/7"> C++(GCC) 在线工具 </a> </li> <li> <a target="_blank" title="Go 在线工具" href="/Onlinetools/details/20"> Go 在线工具 </a> </li> <li> <a target="_blank" title="Fortran 在线工具" href="/Onlinetools/details/45"> Fortran 在线工具 </a> </li> </ul> </div> </div> </div> <script type="text/javascript">var eskeys = 'application.html.erb,仍未,呈现'; var cat = 'cc';';//other-dev</script> </div> <div id="pop" onclick="pophide();"> <div id="pop_body" onclick="event.stopPropagation();"> <h6 class="flex flex101"> 登录 <span onclick="pophide();">关闭</span> </h6> <div class="pd-1"> <div class="wxtip center"> <span>扫码关注<em>1秒</em>登录</span> </div> <div class="center"> <img id="qr" src="https://huajiakeji.com/Content/Images/qrydx.jpg" alt="" style="width:150px;height:150px;" /> </div> <div style="margin-top:10px;display:flex;justify-content: center;"> <input type="text" placeholder="输入验证码" id="txtcode" autocomplete="off" /> <input id="btngo" type="button" onclick="chk()" value="GO" /> </div> <div class="center" style="margin: 4px; font-size: .8rem; color: #f60;"> 发送“验证码”获取 <em style="padding: 0 .5rem;">|</em> <span style="color: #01a05c;">15天全站免登陆</span> </div> <div id="chkinfo" class="tip"></div> </div> </div> </div> <script type="text/javascript" src="https://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script> <script type="text/javascript" src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script> <script type="text/javascript" src="https://img01.yuandaxia.cn/Scripts/highlight.min.js"></script> <script type="text/javascript" src="https://img01.yuandaxia.cn/Scripts/base.js?v=0.22"></script> <script type="text/javascript" src="https://img01.yuandaxia.cn/Scripts/tui.js?v=0.11"></script> <footer class="footer"> <div class="container"> <div class="flink mb-1"> 友情链接: <a href="https://www.it1352.com/" target="_blank">IT屋</a> <a href="https://huajiakeji.com/" target="_blank">Chrome插件</a> <a href="https://www.cnplugins.com/" target="_blank">谷歌浏览器插件</a> </div> <section class="copyright-section"> <a href="https://www.it1352.com" title="IT屋-程序员软件开发技术分享社区">IT屋</a> ©2016-2022 <a href="http://www.beian.miit.gov.cn/" target="_blank">琼ICP备2021000895号-1</a> <a href="/sitemap.html" target="_blank" title="站点地图">站点地图</a> <a href="/Home/Tags" target="_blank" title="站点标签">站点标签</a> <a target="_blank" alt="sitemap" href="/sitemap.xml">SiteMap</a> <a href="/1155981.html" title="IT屋-免责申明"><免责申明></a> 本站内容来源互联网,如果侵犯您的权益请联系我们删除. </section> <!--统计代码--> <script type="text/javascript"> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?0c3a090f7b3c4ad458ac1296cb5cc779"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <script type="text/javascript"> (function () { var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </div> </footer> </body> </html>