incomplete "\n" on UTF-16LE Error - ruby-on-rails

I have been trying to follow several tutorials on rails and each time I am hitting the following issue at the time of running rails s:
Started GET "/" for 127.0.0.1 at 2014-09-14 06:57:44 +0100
Connecting to database specified by database.yml
Processing by CarsController#index as HTML
Car Load (1.0ms) SELECT "cars".* FROM "cars"
Rendered cars/index.html.erb within layouts/application (396.0ms)
Completed 500 Internal Server Error in 101655ms
ActionView::Template::Error (incomplete "\n" on UTF-16LE
(in /cygdrive/c/rails/todolist/3/rails-angular-example-master/app/assets/javascripts/angular_app.js.coffee.erb)):
3: <head>
4: <title>Angular</title>
5: <%= stylesheet_link_tag "application", :media => "all" %>
6: <%= javascript_include_tag "application" %>
7: <%= csrf_meta_tags %>
8: </head>
9: <body>
app/views/layouts/application.html.erb:6:in `_app_views_layouts_application_html_erb__988651031__1054838708'
app/controllers/cars_controller.rb:6:in `index'
config/initializers/quiet_assets.rb:8:in `call_with_quiet_assets'
Tutorial:https://github.com/wulftone/rails-angular-example

This occurs when ExecJS tries to process your assets (javascript and coffeescript) and your file encoding is not UTF-16LE.
Most popular fix: use an alternative ExecJS runtime
Probably the most popular fix is to use NodeJS as your ExecJS runtime. One way to do so:
Install nodejs
Set an environment varaible EXECJS_RUNTIME to Node
Start up your Rails server
Alternative fix: modify existing configuration
Alternative to making another installation (NodeJS) is to modify the configuration of your default ExecJS runtime. I believe your system is using cscript.
Have a look at your execjs runtimes.rb file. (It is in your gems directory.) You can see the latest version of this file on github here. It contains the following:
JScript = ExternalRuntime.new(
name: "JScript",
command: "cscript //E:jscript //Nologo //U",
runner_path: ExecJS.root + "/support/jscript_runner.js",
encoding: 'UTF-16LE' # CScript with //U returns UTF-16LE
)
I can't advise you on the best course to take, but for the sake of getting a fix in place quickly, you may want to just edit this file (then restart your server).
You may find that removing the //U fixes the problem.
You may find that changing UTF-16LE to UTF-8 fixes the problem.
You may find that the combination of both of these steps fixes the problem.
These instructions are drawn from ExecJS::RuntimeError on Windows trying to follow rubytutorial

Related

Why are images not working after migrating Rails 7 app from Heroku to Railway? (S3 images not showing, new image upload crashes app)

I have a Rails 7 app with Postgresql database and AWS S3 image storage. It's currently deployed on Heroku and I want to migrate to Railway (https://railway.app/).
I have successfully deployed the app and migrated the database to Railway, and everything is working fine EXCEPT images. The ones already uploaded to S3 aren't showing up, and when I try to add a new image, the app crashes and I get the following error:
ActionView::Template::Error (You must have ImageMagick or GraphicsMagick installed)
I have tried reinstalling ImageMagick but it doesn't do anything. Everything is still working fine on the Heroku deployment.
In addition, every time I deploy on Railway, it says I don't have Ruby 3.2.1 installed, so it reinstalls it each time and deployment takes about 6 minutes (instead of 30 seconds on Heroku). I get this warning:
Warning! PATH is not properly set up, /usr/local/rvm/gems/3.1.2/bin is not available.
Could they be related somehow?
Could there be something to do with AWS S3 that makes it so the images don't show up?
Is there something I need to do to install ImageMagick and Ruby 3.2.1 directly into Railway? Why does it not recognize them?
Update:
I added the following to my variables, and switched to the Heroku buildpack (in Railway) per this article and this discord thread.
NIXPACKS_PKGS=imagemagick
NIXPACKS_APT_PKGS=libimagemagick-dev
Now I no longer get the Ruby 3.2.1 path warning. I am able to add an image BUT it seems like it's only in local storage. When I refresh the app crashes again, with an empty error message in the logs:
I, [2022-11-02T06:31:25.034636 #120] INFO -- : [8a1f130e-6718-4a29-9f9f-d3a27f200e35] Completed 500 Internal Server Error in 7025ms (ActiveRecord: 94.6ms | Allocations: 106325)
F, [2022-11-02T06:31:25.036160 #120] FATAL -- : [8a1f130e-6718-4a29-9f9f-d3a27f200e35]
[8a1f130e-6718-4a29-9f9f-d3a27f200e35] ActionView::Template::Error (`convert /tmp/ActiveStorage-118-20221102-120-zx3huu.jpg[0] -auto-orient -resize 150x150> /tmp/image_processing20221102-120-yfhkye.jpg` failed with error:
):
[8a1f130e-6718-4a29-9f9f-d3a27f200e35] 54: <% if review.images.exists? %>
[8a1f130e-6718-4a29-9f9f-d3a27f200e35] 55: <% review.images.each do |image| %>
[8a1f130e-6718-4a29-9f9f-d3a27f200e35] 56: <% if review.images.attached? && image.content_type.in?(%w[image/gif image/jpeg image/png]) %>
[8a1f130e-6718-4a29-9f9f-d3a27f200e35] 57: <%= link_to image_tag(review.image_as_thumbnail(image)), image, class: "img-thumbnail border-0 px-0", target: :_blank %>
[8a1f130e-6718-4a29-9f9f-d3a27f200e35] 58: <% end %>
[8a1f130e-6718-4a29-9f9f-d3a27f200e35] 59: <% end %>
[8a1f130e-6718-4a29-9f9f-d3a27f200e35] 60: <% end %>
2nd Update
It seems like it's happening only for images I already uploaded. Is it possible that Heroku adds something to images uploaded to ensure they're unique, that I would have to do in the code instead when using Railway?

Rails: Assets cannot be found in production mode, although being served properly by caddy

I have been trying to fix this problem for almost days now.
I am trying to run my ruby on rails site in production mode, but (some of) the assets cannot be served. The images are located in a custom subfolder ( app/assets/audio/testfolder/demo.png ) which is added to Rails.application.config.assets.paths and precompiled properly but I am always getting the error
ActionView::Template::Error (The asset "demo.png" is not present in the asset pipeline.)
or to be more specific:
I, [2017-09-25T00:38:32.859541 #41037] INFO -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] Started GET "/" for 127.0.0.1 at 2017-09-25 00:38:32 +0200
I, [2017-09-25T00:38:32.860377 #41037] INFO -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] Processing by TestControllerController#index as HTML
I, [2017-09-25T00:38:32.861240 #41037] INFO -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] Rendering test_controller/index.html.erb within layouts/application
I, [2017-09-25T00:38:32.861558 #41037] INFO -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] Rendered test_controller/index.html.erb within layouts/application (0.1ms)
I, [2017-09-25T00:38:32.863790 #41037] INFO -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] Completed 500 Internal Server Error in 3ms
F, [2017-09-25T00:38:32.864505 #41037] FATAL -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1]
F, [2017-09-25T00:38:32.864789 #41037] FATAL -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] ActionView::Template::Error (The asset "demo.png" is not present in the asset pipeline.):
F, [2017-09-25T00:38:32.865058 #41037] FATAL -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] 10:
[dd7b2824-614f-4e55-ba32-83548f79b2e1] 11: <body>
[dd7b2824-614f-4e55-ba32-83548f79b2e1] 12: <%= yield %>
[dd7b2824-614f-4e55-ba32-83548f79b2e1] 13: <%= image_tag(yield(:img)) %>
[dd7b2824-614f-4e55-ba32-83548f79b2e1] 14: <audio src="<%= audio_path(yield(:audio))%>" type="audio/mpeg" controls preload="none"></audio>
[dd7b2824-614f-4e55-ba32-83548f79b2e1] 15: </body>
[dd7b2824-614f-4e55-ba32-83548f79b2e1] 16: </html>
The image can be accessed by localhost:81/assets/demo-57cec01fc3b0b102085dc4de14576f8fd86f8298aed121ce17245df0546276d4.png directly, so the webserver (caddy) does not seem to be the problem.
I hosted a simple demo project on github ( https://github.com/Kafkalasch/RailsAssetsErrorDemo ) to reproduce the problem easily.
Just clone the project, precompile the assets
rails assets:precompile
, start the caddy server with the given Caddyfile,
caddy -conf Caddyfile
and start puma with config/puma.production.rb
puma -C RubyDemo/config/puma.production.rb
I am using:
ruby 2.4.1
puma 3.10.0
rails 5.1.4
caddy 0.10.6
I could reproduce this problem on Debian and Mac OS X, so I don't assume that the OS is relevant.
I want to use my webserver to serve static files, so I am looking for an other solution than simply setting
config.assets.compile = true
Does anyone have an idea or any hint how I could solve my problem?
I am grateful for any response.
I tested your app with puma webserver in production and development.
I solved the error by including the folder for the mp3 and png files in your index.html.erb file.
<% provide(:img, "testfolder/demo.png") %>
<% provide(:audio, "testfolder/demo.mp3") %>
because your images and assets are precompiled inside public/assets/testfolder instead of public/assets
The problem is solved

How do I specify a stylesheet for my HTML email with Premailer-Rails gem?

I have a stylesheet I want to use in my HTML emails at the path:
app/vendor/assets/stylesheets/inspinia/email_templates/email-styles.css
I am using the Premailer-Rails gem for my email styling.
In my mailer.html.erb layout, I have the following:
<%= stylesheet_link_tag "inspinia/email_templates/email-styles.css", media: 'all' %>
However, in my logs on Heroku, I get the following message:
app[web.1]: [923202d9-b3f1-4b9a-9f42-7b70dcc01d92] Started GET "/stylesheets/inspinia/email_templates/email-styles.css" for 54.167.56.21 at 2016-12-23 23:25:08 +0000
app[web.1]: [923202d9-b3f1-4b9a-9f42-7b70dcc01d92]
2016-12-23T23:25:08.286725+00:00 app[web.1]: [923202d9-b3f1-4b9a-9f42-7b70dcc01d92] ActionController::RoutingError (No route matches [GET] "/stylesheets/inspinia/email_templates/email-styles.css"):
So how do I figure out/specify the correct path for this stylesheet in production?
try move email-styles.css to /public/assets/ folder, or add it to the assets pipeline.
Premailer-rails wiki:
File System: If there's a file inside public/ with the same path as in the URL, it is read from disk. E.g. if the URL is http://cdn.example.com/assets/email.css the contents of the file located at public/assets/email.css gets returned if it exists.
Asset Pipeline: If Rails is available and the asset pipeline is enabled, the file is retrieved through the asset pipeline. E.g. if the URL is http://cdn.example.com/assets/email-fingerprint123.css, the file email.css is requested from the asset pipeline. That is, the fingerprint and the prefix (in this case assets is the prefix) are stripped before requesting it from the asset pipeline.
Add the following into config/initializers/assets.rb:
Rails.application.config.assets.precompile << 'email-styles.css'
Rails.application.config.assets.paths << Rails.root.join('app/vendor/assets/stylesheets/inspinia/email_templates')
Then use <%= stylesheet_link_tag 'email-styles.css', media: 'all' %> in the template.

Issue after I deploy the app to heroku

I have a web app I'm currently working on, I just came across of an issue right after the system finishes executing git push heroku master. In development, the app works fine. I've already changed the db to PostgreSQL, so there's no issue to push. But after the push, when I try to see the app online, it tells me there's an error, when I search the heroku logs via the command Heroku logs --tail, here's what I found for errors:
Completed 500 Internal Server Error in 866ms
at parse_error (/tmp/execjs20130330-2-1s2w7pb.js:1824:17)
at new JS_Parse_Error (/tmp/execjs20130330-2-1s2w7pb.js:1720:22)
ActionView::Template::Error (Unexpected character '#' (line: 11021, col: 1, pos: 312418)
Error
at js_error (/tmp/execjs20130330-2-1s2w7pb.js:1728:15)
at Object.semicolon [as 1] (/tmp/execjs20130330-2-1s2w7pb.js:2221:38)
at simple_statement (/tmp/execjs20130330-2-1s2w7pb.js:2362:35)
at /tmp/execjs20130330-2-1s2w7pb.js:2265:32
at Object.next_token [as input] (/tmp/execjs20130330-2-1s2w7pb.js:2070:17)
at next (/tmp/execjs20130330-2-1s2w7pb.js:2175:37)
at /tmp/execjs20130330-2-1s2w7pb.js:2753:32
(in /app/app/assets/javascripts/application.js)):
9: <body>
6: <%= javascript_include_tag
4: <title>Pinsvault</title>
I can't see where to go to fix, it seems something with JavaScript, but I don't have anything like that in development.
Looks like you're not closing your javascript_include_tag, try adding %>
<%= javascript_include_tag %>
You can try to verify this locally with the Chrome inspector's console tab. See if you have any javascript errors there.

Windows - JRuby on Rails - ActionView::Template::Error invalid encoding

I want to try jruby 1.7.0.RC2 on Windows with Rails 3.2. I'm able to start the server, but each time I try to access a page that requires a css (javascript_include_tag), I get the following error:
invalid encoding in marshaling stream: I"co
Here is the stack trace, if it helps:
ActionView::Template::Error (invalid encoding in marshaling stream: I"◄co):
9: = stylesheet_link_tag "application", :media => "all"
app/views/layouts/application.html.haml:9:in `_app_views_layouts_application_h
tml_haml__357561048_13162'
Rendered C:/jruby-1.7.0.RC2/lib/ruby/gems/shared/gems/actionpack-3.2.8/lib/act
ion_dispatch/middleware/templates/rescues/_trace.erb (7.0ms)
Rendered C:/jruby-1.7.0.RC2/lib/ruby/gems/shared/gems/actionpack-3.2.8/lib/act
ion_dispatch/middleware/templates/rescues/_request_and_response.erb (6.0ms)
Rendered C:/jruby-1.7.0.RC2/lib/ruby/gems/shared/gems/actionpack-3.2.8/lib/act
ion_dispatch/middleware/templates/rescues/template_error.erb within rescues/layo
ut (41.0ms)
My application.css is only the result of the default asset pipeline, nothing fancy. I even get the same error with an empty application.css.scss file...
Everything works fine with Ruby MRI 1.9.2 and 1.9.3.
I must say that when it comes to encoding, I'm clueless. Any idea?
Running rake tmp:clear in the project root dir fixed this issue for me. It seems that jRuby does not like to use MRI Ruby's cached files.

Resources