简体   繁体   English

我可以从我的 controller 渲染 rails 欢迎页面吗?

[英]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::WelcomeControllerindex方法,并呈现默认的欢迎页面。

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

相关问题 我可以在Rails 4中从当前控制器渲染另一个控制器的动作吗? - Can I render another controller's action from my current controller in Rails 4? 我可以在 rails 的单个页面中呈现两个不同的控制器操作吗 - Can i render two different controller actions in a single page in rails 如何在Rails中使用对象的控制器的create动作用对象的新实例渲染局部对象? - How can I render a partial with a new instance of my object from the create action of that object's controller in Rails? 如何在Rails 4上将控制器中的文本渲染为html? - How can I render text from a controller as html on Rails 4? 无法从 Controller 呈现页面以在 Rails 中查看 - Not able to render page from Controller to view in Rails 我可以检查控制器是否从 Rails 中的 AMP 页面调用? - Can I Check if a Controller is Being Called from an AMP page in Rails? Rails-我可以从ActionCable coffeescript中调用控制器方法吗? - Rails - can I call a controller method from my ActionCable coffeescript? Rails 6:仅当我使用我的控制器中的方法时才渲染部分内容 - Rails 6: Render a partial if only I'm using a method from my controller 轨道如何渲染来自另一个控制器的页面 - rails how to render a page which is from another controller 如何在没有原型助手的情况下从rails控制器渲染局部页面 - How to render partial in page from rails controller without prototype helpers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM