问题
I think there is a big problem with JavaScript files in Rails 5 apps in Production environment on Heroku. It works locally though
My configuration:
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
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.
Step 1: Creating a new Rails 5 App and switching to the root folder
> rails new my_app --database=postgresql
> cd my_app
Step 2: Generate a controller
> rails generate controller static_pages
Step 3: edit the /app/controllers/static_pages_controller file
class StaticPagesController < ApplicationController
def show
end
def destroy
end
end
Step 4: create the views to show with the actions
/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
Step 5: edit the /config/routes.rb file
Rails.application.routes.draw do
root 'static_pages#show'
get 'static_pages/show'
delete '/logout', to: 'static_pages#destroy'
end
Step 6: check the routes
> 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
Step 7: create the DB and migrate
> rails db:create
Created database 'my_app_development'
Created database 'my_app_test'
> rails db:migrate
Step 8: start the server
> rails server
Step 9: go to localhost:3000/ and click the link presented. the link works and send a delete action to the server
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)
Step 10: create a heroku app(you have to be logged in to heroku on your system)
> heroku create
Step 11: push your work to Heroku
> git add -A
> git commit -m "init"
> git push --set-upstream heroku master
Step 12: open your Heroku app and do like in Step 9 and fail because JavaScript seems to not work correctly
(...) 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"):
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.
Here are the links if you want to check out all the sources for the app and the app deployed to heroku.
You can check the source code here:BitBucket Repo
And the app here: Heroku app
Does anyone else has this hardships with Rails and Heroku or is there anything I can do to make it work?
EDIT: made the output of Step 12 a bit clearer by deleting unneccessary log information and time stamps
回答1:
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.
Now the JS runs perfectly fine. Here is the change I did in the GemFile
to make it work:
From
gem 'uglifier', '>= 1.3.0'
to
gem 'uglifier', '~> 3.0.4'
The uglifier
gem version was 4.1.18
that caused this JS error in production environment.
Hope this helps others to pin down their JS errors on Heroku
回答2:
The issue is the duktape
gem. That is a Javascript engine that's not widely used in the Rails world, but is included by default in a new Windows rails installation, because it's one of the only engines that actually works in Windows. (If you haven't figured out by now, not a lot of Rails windows devs :) ).
I don't think Heroku has great support for it, so when it picks up that engines, the JS silently fails to compile. If you look into the application.js
file in production, you'll see a bunch of NaN: "select[data-remote], input[data-remote], textarea[data-remote]", at the top, when you should see things like
inputChangeSelector: "select[data-remote], input[data-remote], textarea[data-remote]"`.
If you want to get things working, move the Duktape
gem out of its current spot to the development
section of your Gemfile, bundle, and redeploy (you can avoid a JS compiler in the main Gemfile for now.
I would also open a ticket with Heroku. Generally, they warn you of common config issues such as this, but when I tried to deploy your app, I got no such warning. I would imagine just hasn't come up for a lot of users.
来源:https://stackoverflow.com/questions/52154149/javascript-in-a-fresh-rails-5-2-1-app-on-heroku-doesnt-work-properly