Rails 当 public 目录下存在 index.html 时路径请求问题

zouyu · 2016年05月20日 · 最后由 jiyuhang110 回复于 2016年05月23日 · 2528 次阅读

在 Rails 开发环境下 (rails server 启动) 此时,如果 public 目录下有 index.html。当访问路径如下时: http://localhost:3000/ http://localhost:3000/index http://localhost:3000/index.html 为什么会优先访问 public/index.html?

看下 routes 里的 root 指向哪儿,或许可解。

#1 楼 @qinfanpeng 从 server 的 log 看没有走到 routes

Anything in the public directory is rendered as a static asset, so no Ruby code in there.

Instead, you should remove the index.html file in that directory, and replace it with something else within your app. For example, you can do something like this:

# app/controllers/site_controller.rb
class SiteController < ApplicationController
  def index
    # ...
  end
end

# config/routes.rb
root to: 'site#index'

# app/views/site/index.html.erb
<!-- Your HTML and Ruby here -->
This will give you the behavior you want.

http://stackoverflow.com/questions/18485429/rails-use-ruby-in-public-index

#3 楼 @qinfanpeng 看来优先使用 public 下的了 谢谢

#4 楼 @zouyu 除了先使用的 public 的问题,还有就是这样弄个它是纯静态的,不在 Rails 的控制之下。

nginx 的配置文件,里面有个 tryfile,你搜索下它的意思。调整一下$uri/index 的位置,或删除它,估计可以解决你的问题

需要 登录 后方可回复, 如果你还没有账号请 注册新账号