代码之家  ›  专栏  ›  技术社区  ›  Ilja KO

Heroku上新的rails5.2.1应用程序中的Javascript无法正常工作

  •  1
  • Ilja KO  · 技术社区  · 7 年前

    我的配置:

    Windows:   10 Education
    Rails:     5.2.1
    Ruby:      2.4.4p296
    Heroku:    heroku/7.7.7 win32-x64 node-v10.7.0
    postgres:  postgres (PostgreSQL) 10.3
    

    > rails new my_app --database=postgresql
    > cd my_app
    

    第二步:生成控制器

    > rails generate controller static_pages
    

    步骤3:编辑/app/controllers/static\u pages\u controller文件

    class StaticPagesController < ApplicationController
      def show
      end
    
      def destroy
      end
    end
    

    /应用程序/视图/静态页面/显示.html.erb:

    this is the show view
    
    here is a link to the delete action:
    
    <%= link_to t('logout'), logout_path, method: :delete %>
    

    /应用程序/视图/静态页面/销毁.html.erb:

    this is the destroy view which is called with a link_to method: :delete
    

    第5步:编辑/config/路由.rb文件

    Rails.application.routes.draw do
      root 'static_pages#show'
      get 'static_pages/show'
      delete '/logout', to: 'static_pages#destroy'
    end
    

    > rails routes
                       Prefix Verb   URI Pattern                                                                              Controller#Action
                         root GET    /                                                                                        staticpages#show
             staticpages_show GET    /staticpages/show(.:format)                                                              staticpages#show
                       logout DELETE /logout(.:format)                                                                        staticpages#destroy
           rails_service_blob GET    /rails/active_storage/blobs/:signed_id/*filename(.:format)                               active_storage/blobs#show
    rails_blob_representation GET    /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show
           rails_disk_service GET    /rails/active_storage/disk/:encoded_key/*filename(.:format)                              active_storage/disk#show
    update_rails_disk_service PUT    /rails/active_storage/disk/:encoded_token(.:format)                                      active_storage/disk#update
         rails_direct_uploads POST   /rails/active_storage/direct_uploads(.:format)                                           active_storage/direct_uploads#create
    

    步骤7:创建数据库并迁移

    > rails db:create
    Created database 'my_app_development'
    Created database 'my_app_test'
    
    > rails db:migrate
    

    第8步:启动服务器

    > rails server
    

    步骤9:转到本地主机:3000/然后单击显示的链接。链接工作并向服务器发送删除操作

    Started GET "/" for 127.0.0.1 at 2018-09-03 18:43:52 +0200
    Processing by StaticPagesController#show as HTML
      Rendering static_pages/show.html.erb within layouts/application
      Rendered static_pages/show.html.erb within layouts/application (12.2ms)
    Completed 200 OK in 504ms (Views: 486.7ms | ActiveRecord: 0.0ms)
    
    
    Started DELETE "/logout" for 127.0.0.1 at 2018-09-03 18:43:57 +0200
    Processing by StaticPagesController#destroy as HTML
      Parameters: {"authenticity_token"=>"10NHmV8N4tF3O0r/YYtKtKmHm3xthGjPAE51osb7L9skCM5ZoM2RoiCtZD4Crh9d69ndTOeNRMmIW28ipI/z9A=="}
      Rendering static_pages/destroy.html.erb within layouts/application
      Rendered static_pages/destroy.html.erb within layouts/application (0.0ms)
    Completed 200 OK in 90ms (Views: 73.0ms | ActiveRecord: 0.0ms)
    

    第10步:创建heroku应用程序(您必须在系统上登录heroku)

    > heroku create
    

    第11步:把你的工作推给Heroku

    > git add -A
    > git commit -m "init"
    > git push --set-upstream heroku master
    

    第12步:打开你的Heroku应用程序,像第9步一样操作,但是失败了,因为JavaScript似乎不能正常工作

    (...) Started GET "/" for 84.147.254.28 at 2018-09-03 16:47:44 +0000
    (...) Processing by StaticPagesController#show as HTML
    (...)   Rendering static_pages/show.html.erb within layouts/application
    (...)   Rendered static_pages/show.html.erb within layouts/application (9.2ms)
    (...) Completed 200 OK in 27ms (Views: 13.7ms)
    .
    .
    . 
    (...) Started GET "/logout" for 84.147.254.28 at 2018-09-03 16:48:05 +0000
    (...) ActionController::RoutingError (No route matches [GET] "/logout"):
    

    这要么是Rails上的一个明显错误,要么是Heroku方面的一个明显错误,因为我所做的只是jsut创建一个新的Rails应用程序并更改了路由,生成了一个控制器并为控制器操作创建了两个视图。

    如果您想查看应用程序和部署到heroku的应用程序的所有源代码,请参阅以下链接。

    BitBucket Repo

    这里的应用程序: Heroku app

    编辑 :通过删除不必要的日志信息和时间戳,使步骤12的输出更加清晰

    2 回复  |  直到 7 年前
        1
  •  2
  •   Ilja KO    7 年前

    我解决了我的问题:那是 uglifier 由于工作不正常而导致问题的gem。我只是一个初学者,回顾这个gem应该是我需要检查的第一件事,因为它做的JS压缩。

    GemFile 要使其工作:

    gem 'uglifier', '>= 1.3.0'
    

    gem 'uglifier', '~> 3.0.4'
    

    丑陋的 宝石版是 4.1.18

    希望这能帮助其他人在Heroku上找到JS错误

        2
  •  1
  •   chrismanderson    7 年前

    问题是 duktape 宝石。这是一个Javascript引擎,在Rails世界中没有广泛使用,但是在新的Windows Rails安装中默认包含了它,因为它是在Windows中实际工作的唯一引擎之一。(如果您现在还没有弄清楚,那么Rails和windows开发人员并不多:))。

    我不认为Heroku对它有很大的支持,所以当它使用这个引擎时,JS会默默地编译失败。如果你看看 application.js NaN: "select[data-remote], input[data-remote], textarea[data-remote]", at the top, when you should see things like inputChangeSelector:“选择[data remote],输入[data remote],文本区[data remote]”。

    如果你想让事情顺利进行,就移动 Duktape 宝石走出目前的现货市场 development 您的Gemfile、bundle和redeploy部分(您现在可以避免在主Gemfile中使用JS编译器。

    我也会在Heroku开一张票。一般来说,它们会警告您类似这样的常见配置问题,但当我尝试部署您的应用程序时,却没有收到此类警告。我可以想象,只是没有出现了很多用户。

    推荐文章