Ruby-on-rails 我的应用程序中默认的“欢迎登机”页面在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17964830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:26:47  来源:igfitidea点击:

Where is the default "Welcome Aboard" page located in my app?

ruby-on-railsruby-on-rails-4

提问by 7stud

I scoured my app's directories, and I can't find the html page for the default rails Welcome Aboard page. I also cannot find a route for the default Welcome Aboard page in routes.rb. How does my rails app route http://localhost:3000/to a non-existent page in my app?

我搜索了我的应用程序目录,但找不到默认 rails Welcome Aboard 页面的 html 页面。我也无法在 routes.rb 中找到默认欢迎登机页面的路线。我的 rails 应用程序如何路由http://localhost:3000/到我的应用程序中不存在的页面?

The rails server produces this information:

rails 服务器生成以下信息:

Started GET "/" for 127.0.0.1 at 2013-07-31 02:00:13 -0600
Processing by Rails::WelcomeController#index as HTML
  Rendered /Users/7stud/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/railties-4.0.0/lib/rails/templates/rails/welcome/index.html.erb (0.1ms)
Completed 200 OK in 3ms (Views: 2.5ms | ActiveRecord: 0.0ms)

So it looks to me like there is a controller buried in a gem somewhere that handles the request.

所以在我看来,有一个控制器埋在处理请求的宝石中。

回答by Jakob S

Since Rails 4, the "Welcome aboard" page is no longer located in public/index.html. It is - as you've already detected - located inside one of the Rails gems.

从 Rails 4 开始,“欢迎登机”页面不再位于public/index.html. 它 - 正如您已经发现的 - 位于 Rails gems 内。

So you already answered the question yourself; the "Welcome aboard" page is - in your case - located at /Users/7stud/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/railties-4.0.0/lib/rails/templates/rails/welcome/index.html.erb

所以你已经自己回答了这个问题;“欢迎登机”页面 - 就您而言 - 位于/Users/7stud/.rvm/gems/ruby-2.0.0-p247@railstutorial_rails_4_0/gems/railties-4.0.0/lib/rails/templates/rails/welcome/index.html.erb

To get rid of it, following the instructions on the page. Basically they are:

要摆脱它,请按照页面上的说明进行操作。基本上它们是:

  1. Create a controller
  2. Add a root route in config/routes.rbto route to that newly created controller.
  1. 创建控制器
  2. 添加根路由config/routes.rb以路由到新创建的控制器。

As for how the request to your application ends up at a controller inside railties, let's dig into the gem: Inside Rails::Application::Finisherwe find this:

至于对您的应用程序的请求如何最终到达 railties 中的控制器,让我们深入研究 gem:在里面Rails::Application::Finisher我们发现:

initializer :add_builtin_route do |app|
  if Rails.env.development?
    app.routes.append do
      get '/rails/info/properties' => "rails/info#properties"
      get '/rails/info/routes'     => "rails/info#routes"
      get '/rails/info'            => "rails/info#index"
      get '/'                      => "rails/welcome#index"
    end
  end
end

This block adds a few routes to your application when running in development mode - one of those is the route to the "Welcome aboard" action: get '/' => "rails/welcome#index"

在开发模式下运行时,此块为您的应用程序添加了一些路由 - 其中之一是“欢迎登机”操作的路由: get '/' => "rails/welcome#index"

This - like any other initializer - is done when your start your application server (running rails serveror however you do it). In the case of Finisher, all its initializer are run after all other initializers are run.

这 - 就像任何其他初始化程序 - 在您启动应用程序服务器(运行rails server或以任何方式执行)时完成。在 的情况下Finisher,它的所有初始化器都在所有其他初始化器运行之后运行。

Note how the routes are appended so that they are appear last in the Routeset. This, combined with the fact that Rails uses the first matching route it finds, ensures those default routes will only get used if no other route is defined.

请注意路由是如何附加的,以便它们出现在路由集中的最后。这与 Rails 使用它找到的第一个匹配路由的事实相结合,确保只有在没有定义其他路由的情况下才会使用这些默认路由。