[英]Can I render rails welcome page from my controller?
When I want to show default welcome page by routes.rb, I can add route like this:当我想通过 routes.rb 显示默认欢迎页面时,我可以像这样添加路由:
root "rails/welcome#index"
Then, I want to render the same template in my controller out of curiosity.然后,出于好奇,我想在我的 controller 中渲染相同的模板。 The template is in
rails/railties/lib/rails/templates/rails/welcome/index.html.erb
模板位于
rails/railties/lib/rails/templates/rails/welcome/index.html.erb
I thought `render "controller/action" works for my own files, but it doesn't seem to work for built-in files.我认为`render "controller/action" 适用于我自己的文件,但它似乎不适用于内置文件。
def index
render "rails/welcome/index" # This shows error `Missing template rails/welcome/index`.
end
So, Is there a handy way to render built-in template?那么,有没有一种方便的方法来呈现内置模板?
Yes you can render the rails welcome page from your controller by using the Rails::WelcomeController
是的,您可以使用
Rails::WelcomeController
从 controller 呈现 rails 欢迎页面
class WelcomeController < Rails::WelcomeController
end
Don't define the index
method in WelcomeController
.不要在
WelcomeController
中定义index
方法。 Rails will pick the index
method of Rails::WelcomeController
and will render the default welcome page. Rails 将选择
Rails::WelcomeController
的index
方法,并呈现默认的欢迎页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.