![](/img/trans.png)
[英]Can I render another controller's action from my current controller in Rails 4?
[英]Can I render rails welcome page from my controller?
当我想通过 routes.rb 显示默认欢迎页面时,我可以像这样添加路由:
root "rails/welcome#index"
然后,出于好奇,我想在我的 controller 中渲染相同的模板。 模板位于rails/railties/lib/rails/templates/rails/welcome/index.html.erb
我认为`render "controller/action" 适用于我自己的文件,但它似乎不适用于内置文件。
def index
render "rails/welcome/index" # This shows error `Missing template rails/welcome/index`.
end
那么,有没有一种方便的方法来呈现内置模板?
是的,您可以使用Rails::WelcomeController
从 controller 呈现 rails 欢迎页面
class WelcomeController < Rails::WelcomeController
end
不要在WelcomeController
中定义index
方法。 Rails 将选择Rails::WelcomeController
的index
方法,并呈现默认的欢迎页面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.