【问题标题】:No route matches [GET] /assets没有路线匹配 [GET] /assets
【发布时间】:2011-12-11 09:12:36
【问题描述】:

我有一个 Rails 应用程序,我正在尝试在生产环境中进行测试。我运行了RAILS_ENV=production rake assets:precompile,它在 /public/assets 中生成了我的所有资产。问题是,当我使用RAILS_ENV=production rails s thin 启动我的应用程序时,我得到:

ActionController::RoutingError (No route matches [GET] "/assets/application-eff78fd67423795a7be3aa21512f0bd2.css"):

该文件确实存在于/public/assets/application-eff78fd67423795a7be3aa21512f0bd2.css

对我为什么收到这个RoutingError有任何想法吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 asset-pipeline


    【解决方案1】:

    在生产模式下,Rails 将不负责提供静态资源。因此,您会收到此错误。 Thin 也不会这样做,因为它只是 Rails 的一个包装器。

    这由您应用程序中config/environments/production.rb 中的此设置控制:

    config.serve_static_files = false
    

    或者在 Rails 5 中:

    # config/environments/production.rb
    config.public_file_server.enabled = true
    

    或将ENV['RAILS_SERVE_STATIC_FILES'] 设置为true。

    您可以设置为 true 或使用像 Apache 或 Nginx 这样的真实服务器来提供静态资源。我怀疑 Pow 也可能这样做。


    如果您使用 Heroku,他们建议使用 rails_12factor gem,默认情况下启用此设置。将 gem 放入 Gemfileproduction 组中,如下所示:

    group :production do
      gem 'rails_12factor'
    end
    

    【讨论】:

    • 有人知道这是否是部署到heroku时上述问题的解决方案吗?
    • 明确的答案,非常感谢。当我在我的开发机器上使用thin 测试生产环境时,我进入了这个页面。我正在编译资产,但 application.css 为空,服务器日志给出了 OP 错误。
    • 在 Rails 4 中它将是 config.serve_static_filesconfig.serve_static_assets 已弃用,将在 Rails 5 中删除。
    • 部署到一个 RHEL 发行版,我没有接触默认环境配置,只是在 Rails v4.2.4 中添加了 rails_12factor gem,现在一切正常。非常感谢
    • @Onur:这意味着您的 Rails 应用程序将提供资产,而不是您的网络服务器。我完全不推荐这种配置,因为它会使你的 Rails 服务器变慢。
    【解决方案2】:

    除了 Ryan 上面所说的,Rails 资产管道指南描述了如何设置 Apache 或 nginx 来为您提供静态资产。

    http://guides.rubyonrails.org/asset_pipeline.html

    您确实应该设置 nginx 或 Apache 来提供静态资产,因为它们比 mongrel/thin/unicorn 更适合此任务。

    【讨论】:

      【解决方案3】:

      刚刚解决了同样的问题。就我而言,瑞安的回答没有帮助。 Bratsche 指向 Rails Guides,不幸的是这对我也不起作用。但是,该资源很有帮助。所以我从那里获取了 Nginx 配置并添加了 root 指令,指向公共目录。没有这个就不行了。

         # serve static assets
         location ~ ^/assets/ {
           expires 1y;
           root  /path/to/my/cool_project/public;
           add_header Cache-Control public;
      
           add_header ETag "";
           break;
         }
      

      重启nginx,就这样了。

      【讨论】:

        【解决方案4】:

        在 rails 5 中,config.serve_static_files 选项已更改,所以现在您需要拥有

        config.public_file_server.enabled = true
        

        在本地提供资产。

        【讨论】:

          【解决方案5】:

          确实,您不需要修改任何默认配置。 您只需再次重新编译资产文件

          删除公共/资产

          1.rake assets:clobber RAILS_ENV=production

          资产编译

          2.rake assets:precompile RAILS_ENV=production

          3.重启服务器,例如(nginx)

          【讨论】:

          • @SteveO7,很明显,rails 默认在开发模式下使用资产管道。
          【解决方案6】:

          Rails 4.2 在您的 config/environments/staging.rb 和 production.rb 文件中添加/更改了这一行:

          config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
          

          如果未设置 RAILS_SERVE_STATIC_FILES,并且您是来自 Rails 服务器的服务资产(如 Unicorn),则默认为“false”,并且会发生 RoutingError。

          这是一个简单的解决方法:

          config.serve_static_files = true
          

          【讨论】:

          • 谢谢,这是最简单的选择。配置通常取决于服务器环境,使用 env 变量对其进行配置非常好。
          【解决方案7】:

          试试下面的代码:

          config/environments/production.rb

          config.assets.compile = true
          

          然后运行命令:

          RAILS_ENV=production rake assets:precompile
          

          然后将所有编译文件和清单文件推送到服务器。

          【讨论】:

            【解决方案8】:

            我使用 mina+puma+nginx 来部署我的 Rails 5 应用程序,我得到了

            ActionController::RoutingError (No route matches [GET] "/assets/application-658cf2ab3ac93aa5cb41a762b52cf49d7184509c307922cd3fbb61b237a59c1a.css")
            

            检查 config/environments/production.rb

            # Disable serving static files from the `/public` folder by default since
            # Apache or NGINX already handles this.
            config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
            

            NGINX 已经处理了这个,请正确配置

            upstream puma {
              server unix:///home/deploy/apps/appname/shared/tmp/sockets/appname-puma.sock;
            }
            
            server {
              listen 80 default_server deferred;
              # server_name example.com;
            
              root /home/deploy/apps/appname/current/public;
              access_log /home/deploy/apps/appname/current/log/nginx.access.log;
              error_log /home/deploy/apps/appname/current/log/nginx.error.log info;
            
              location ^~ /assets/ {
                gzip_static on;
                expires max;
                add_header Cache-Control public;
              }
            
              try_files $uri/index.html $uri @puma;
              location @puma {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
            
                proxy_pass http://puma;
              }
            
              error_page 500 502 503 504 /500.html;
              client_max_body_size 10M;
              keepalive_timeout 10;
            }
            

            一切都会好起来的。

            【讨论】:

              【解决方案9】:

              如果有人在测试环境中遇到与我相同的错误,这对我有帮助:

              rails assets:clobber assets:precompile RAILS_ENV=test
              

              然后:

              ps axu | grep your-username
              

              找到spring server进程和他的PID然后通过:

              kill <spring-server-PID>
              

              【讨论】:

                猜你喜欢
                • 2014-10-18
                • 1970-01-01
                • 1970-01-01
                • 2017-05-06
                • 2011-10-29
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2013-01-09
                相关资源
                最近更新 更多