![](/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.