简体   繁体   English

为什么我必须删除rails应用程序中的index.html文件才能将控制器映射到根目录才能显示?

[英]Why do I have to delete the index.html file in a rails app to get the controller mapped to the root to display?

I did a quick search, but couldn't find a direct answer. 我做了一个快速搜索,但找不到直接答案。 New to Rails and just trying to understand why this is the case. Rails的新手,只是想了解为什么会这样。 I'd really appreciate it if someone could just point of the piece of code that shows where it points to index.html if it exists, or, if that's the wrong way of thinking about it, what the right answer is. 我真的很感激,如果有人能够指出代码中指向index.html的代码,如果它存在,或者,如果这是错误的思考方式,那么正确答案是什么。

In Rails 3, railties defines a default middleware stack (railties/lib/rails/application.rb) that lets each type of middleware access the request call. 在Rails 3中,railties定义了一个默认的中间件堆栈(railties / lib / rails / application.rb),它允许每种类型的中间件访问请求调用。 The first module in the stack is ActionDispatch::Static (it can be disabled with config.serve_static_assets). 堆栈中的第一个模块是ActionDispatch :: Static(可以使用config.serve_static_assets禁用它)。 The static middleware module is in ActionPack (actionpack/lib/action_dispatch/middleware/static.rb). 静态中间件模块位于ActionPack中(actionpack / lib / action_dispatch / middleware / static.rb)。 The relevant lines from that are: 相关的方面是:

path   = env['PATH_INFO'].chomp('/')
...
if file_exist?(path)
  return @file_server.call(env)

@file_server is defined above as a Rack::File which is in rack/lib/rack/file.rb. @file_server在上面定义为Rack :: file,它位于rack / lib / rack / file.rb中。 It just reads the file and serves the contents as the body. 它只是读取文件并作为正文提供内容。

So when you delete index.html, the file_exist? 那么当你删除index.html时,file_exist? call fails which just passes the request on to the next middleware and eventually will hit the normal Rails router. 调用失败,只是将请求传递给下一个中间件,最终将命中正常的Rails路由器。

rails will automatically look in the /public folder FIRST before it checks anything. rails会在检查任何内容之前自动查看/ public文件夹FIRST。 It won't even look at any rb code. 它甚至不会查看任何rb代码。 This is its way of serving static pages. 这是它提供静态页面的方式。

Rails is set to look at /public/index.html but that page has some great information on it especially if you are just setting out on your rails journey. Rails设置为查看/public/index.html,但该页面上有一些很好的信息,特别是如果您刚刚开始使用rails之旅。 As you get into it, you'll learn the first thing you do is rm public/index.html after you create your new empty rails app. 当您进入它时,您将学习在创建新的空rails应用程序后,您所做的第一件事就是rm public/index.html

Your quote: "piece of code that shows where it points to index.html if it exists" 你的引用:“一段代码,显示它指向index.html的位置”

I'm not even sure whether your web server even bothers to run a single line of Rails code if it just can pass on a static file. 我甚至不确定你的Web服务器是否甚至无法运行单行Rails代码,如果它只能传递一个静态文件。 It would be totally inefficient to run Rails code before returning images, static html etc. to the web browser. 在将图像,静态html等返回到Web浏览器之前运行Rails代码是完全没有效率的。

I don't know the details of the various Rails implementations there are, but I think a good way to think about it is that the webserver will dispatch a request to Rails only when it doesn't find the (static) file on its filesystem. 我不知道各种Rails实现的细节,但我认为考虑它的一个好方法是, 只有当它没有在文件系统上找到(静态)文件 ,Web服务器才会向Rails发送请求。 The request is then passed on to Rails as if it were a File Not found error (without sending the 404 status though)... and then Rails can start to match the URL to its routes file. 然后将请求传递给Rails,就好像它是一个File Not found错误(虽然没有发送404状态)......然后Rails可以开始将URL与其路由文件匹配。

暂无
暂无

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

相关问题 如何重定向到 root - public/index.html? - How do I redirect to root - public/index.html? 找不到Rails应用程序乘客nginx index.html”(2:找不到此类文件或目录) - Rails app passenger nginx index.html" is not found (2: No such file or directory) 新手问题:必须在浏览器中键入localhost / controller / index才能使索引方法显示在Rails中。 为什么? - Newbie Question:Have to type localhost/controller/index in browser to get index method to display in Rails. Why? 你如何从index.html.erb而不是index.html开始使用Rails项目? - How do you get a Rails project to start with index.html.erb instead of index.html? 如何使public / index.html与另一个root_path一起使用? - can I get public/index.html to work alongside another root_path? 为什么我对 Rails 后端的调用会导致我的 React 抓取 public/index.html 文件? - Why is my call to my rails back end causing my React to grab the public/index.html file? Rails 3.为什么Capistrano将index.html文件添加到我的公用文件夹中? - Rails 3. Why is Capistrano adding an index.html file to my public folder? 如何防止Rails呈现public / index.html? - How do I prevent rails from rendering public/index.html? 如何在Ruby on Rails应用程序中更改Index.html的头部? - How to change the head section of Index.html in a Ruby on Rails app? Ruby On Rails:如何创建到public / index.html静态文件的路由? - Ruby On Rails: How can I create a route to the public/index.html static file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM