Heroku上新的Rails 5.2.1应用程序中的Javascript无法正常运行 [英] Javascript in a fresh Rails 5.2.1 app on Heroku doesn't work properly
问题描述
我认为Heroku生产环境中的Rails 5应用程序中的JavaScript文件存在很大问题.虽然可以在本地使用
I think there is a big problem with JavaScript files in Rails 5 apps in Production environment on Heroku. It works locally though
我的配置:
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
现在,我将向您介绍我确认该步骤无效的步骤,您可以自己尝试一下.最后,您将在Heroku上找到该Bitbucket存储库和该应用程序的链接.
I will now present you the steps I made to confirm that it doesnt work and you can try for your self. In the end you will have a link to the bitbucket repo and the app on Heroku.
步骤1:创建新的Rails 5应用并切换到根文件夹
> rails new my_app --database=postgresql
> cd my_app
第2步:生成控制器
> rails generate controller static_pages
第3步:编辑/app/controllers/static_pages_controller文件
class StaticPagesController < ApplicationController
def show
end
def destroy
end
end
第4步:创建与操作一起显示的视图
/app/views/static_pages/show.html.erb:
this is the show view
here is a link to the delete action:
<%= link_to t('logout'), logout_path, method: :delete %>
/app/views/static_pages/destroy.html.erb:
this is the destroy view which is called with a link_to method: :delete
第5步:编辑/config/routes.rb文件
Rails.application.routes.draw do
root 'static_pages#show'
get 'static_pages/show'
delete '/logout', to: 'static_pages#destroy'
end
第6步:检查路线
> 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:转到localhost: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应用程序并更改了路线,生成了一个控制器,并为该控制器的动作制作了2个视图.
This is either an obvious error on Rails or on Heroku's side as all you see I did was jsut create a fresh new rails app and changed the routes, generated a controller and made 2 views for the controller actions.
如果您要查看该应用程序的所有源以及部署到heroku的应用程序,请点击此处.
Here are the links if you want to check out all the sources for the app and the app deployed to heroku.
您可以在此处查看源代码: BitBucket存储库
You can check the source code here:BitBucket Repo
此处的应用为: Heroku应用
其他人在Rails和Heroku上是否有这种困难,或者我能做些什么来使其工作?
Does anyone else has this hardships with Rails and Heroku or is there anything I can do to make it work?
编辑:通过删除不必要的日志信息和时间戳使步骤12的输出更加清晰
EDIT: made the output of Step 12 a bit clearer by deleting unneccessary log information and time stamps
推荐答案
我解决了我的问题:是uglifier
gem导致了该问题,因为它无法正常工作.我只是一个初学者,回头看一下这个gem应该是我进行JS压缩时需要检查的第一件事.
I resolved my issue: It was the uglifier
gem that caused the problem as it was not working correctly. I am just a beginner and looking back this gem should have been the first thing I needed to check as it does the JS compression.
现在JS运行得很好.这是我在GemFile
中所做的更改,以使其正常工作:
Now the JS runs perfectly fine. Here is the change I did in the GemFile
to make it work:
来自
gem 'uglifier', '>= 1.3.0'
到
gem 'uglifier', '~> 3.0.4'
uglifier
gem版本是4.1.18
,在生产环境中导致此JS错误.
The uglifier
gem version was 4.1.18
that caused this JS error in production environment.
希望这可以帮助其他人在Heroku上确定其JS错误
Hope this helps others to pin down their JS errors on Heroku
这篇关于Heroku上新的Rails 5.2.1应用程序中的Javascript无法正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!