简体   繁体   English

如何在Rails应用程序中呈现application.html.erb?

[英]How to render application.html.erb in a Rails app?

How can I render the application.html.erb view in a rails app? 如何在Rails应用application.html.erb中呈现application.html.erb视图?

I've tried adding a method to the application_controller.rb controller: 我尝试将方法添加到application_controller.rb控制器:

def amethod
end

And then updating the routes.rb file: 然后更新routes.rb文件:

get '/', to: 'application#amethod'

to no avail. 无济于事。 I get this error message: 我收到此错误消息:

Missing template application/amethod with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. 缺少带有{:locale => [:en] 、: formats => [:html] 、: variants => [] 、: handlers => [:erb,:builder,:raw,:ruby 、:的模板应用程序/方法咖啡,:jbuilder]}。 Searched in: * "/Users/peterward/Directory/mini-project/app/views" 在以下位置搜索:*“ / Users / peterward / Directory / mini-project / app / views”

It's looking for the html.erb view in the app/views directory instead of the app/views/layouts directory. 它正在app/views目录而不是app/views/layouts目录中寻找html.erb视图。

How can I change this? 我该如何更改?

Rails is looking for a file named views/amethod.html.erb , the layout that you're using application.html.erb is not meant to hold the view for your controller action. Rails正在寻找一个名为views/amethod.html.erb的文件,您正在使用application.html.erb的布局并不意味着保留控制器操作的视图。 The layout will yield to the controller action named view. 该布局将产生名为view的控制器动作。

I suggest you read Rail's render and layout guide. 我建议您阅读Rail的渲染和布局指南。

The quickest solution to solving your error would be to create a blank file: app/views/amethod.html.erb 解决错误的最快方法是创建一个空白文件: app/views/amethod.html.erb

However, this does not really address your problem, because I doubt you really just want to render a layout. 但是,这并不能真正解决您的问题,因为我怀疑您确实只想呈现一个布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM