Webpack won't work while trying to build on multiple domain - ruby-on-rails

We are running a Rails 6.1.7 and we want to duplicate our website, while using a new domain and set for it a dedicated theme.scss
In order to have the proper URL being used to load assets we set this in config file:
config.action_controller.asset_host = Proc.new do |source, request|
if source && request && request.ssl?
"#{request.protocol}#{request.host_with_port}"
else
"www.test.local:4000"
end
end
And it works until Webpack need to build after we changed any JS or CSS files.
Once I changed files (CSS or JS) I have an error I don't explain much:
Showing /app/views/layouts/index.erb where line #44 raised:
undefined method `host' for nil:NilClass
Extracted source (around line #53):
51 config.action_controller.default_asset_host = "www.test.local:4000"
52
53 config.action_controller.asset_host = Proc.new do |source, request|
54 if source && request && request.ssl?
55 "#{request.protocol}#{request.host_with_port}"
56 else
We have for index.erb
43 <%= stylesheet_pack_tag 'frontend', media: 'all', 'data-turbolinks-track': 'reload' %>
44 <%= javascript_pack_tag 'frontend', 'data-turbolinks-track': 'reload' %>
45 <%= javascript_include_tag "translations", skip_pipeline: true %>
So what is going wrong here? Any help would be much appreciate!

Related

rails 4.2.8 - stylesheet_link_tag throws ActionView::Template::Error: no implicit conversion of nil into String

I'm trying to update a rails app which runs totally fine if I use rails 4.2.4 (the version it's been developed on) upto rails 4.2.7
As soon as I switch to rails 4.2.8 or later (tested all versions upto 4.2.11) I get
Failure/Error: = stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true
ActionView::Template::Error:
no implicit conversion of nil into String
# ..../actionview-4.2.11/lib/action_view/helpers/asset_url_helper.rb:140:in `join'
# ..../actionview-4.2.11/lib/action_view/helpers/asset_url_helper.rb:140:in `asset_path'
Having a loot at the actionview-4.2.11/lib/action_view/helpers/asset_url_helper, I can see that the error happens in
File.join(relative_url_root, source)
where
pry(#<#<Class:0x00000009a9aa98>>)> source
=> "/assets/application-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css"
pry(#<#<Class:0x00000009a9aa98>>)> relative_url_root
=> {}
As soon as I remove all stylesheet_link_tags and javascript_include_tags, the error does not show up anymore (but of course all my styles and javascripts are gone).
Here's the relevant part of my app/views/layouts/application.html.haml
!!!
%html
%head
%meta{:name => "viewport", :content => "width=device-width, initial-scale=1.0"}
%meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}
%title BrotUndButter
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true
= javascript_include_tag 'application', 'data-turbolinks-track' => true
Any ideas?
Never mind.
This was a very special problem of this very app.
I have been setting a up a custom configuration option in an initializer with
Rails.application.config.x.brotundbutter = Rails::Application::Configuration::Custom.new
and this caused the error. Don't ask me how I found this, it was a pain in the ass.

Rails link_to method with turbolinks disables formatting

I am formatting videos with video_js gem. For example here how I print my videos:
.row
- #streams.each do |stream|
= videojs_rails sources: { mp4: stream.video}, setup: "{}", controls: true, width:"400", class: "col-md-10 col-md-offset-1"
Everything works fine as expected if I go to that page with a link. If I create button with link_to to that page, formatting gets disabled. I need to refresh page to make it look as expected. What's wrong with that? Any ideas?
Remove turbolinks gem from your Gemfile
Run bundle install
In app/views/layouts/application.html.haml set turbolinks to false like that:
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => false
= javascript_include_tag 'application', 'data-turbolinks-track' => false
or remove them.
Restart server

Rails Fails To Generate Correct Link Path In Production Mode

The app whose in production mode is failed to generate correct links.
For instance, the code below
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
is generating the code below:
<link rel="stylesheet" media="all" href="/stylesheets/application.css" data-turbolinks-track="true" />
<script src="/javascripts/application.js" data-turbolinks-track="true"></script>
I precompiled javascript and stylesheet. Their correct path is respectively /public/assets/application-0d1386df5e82480bb54a6cd082e626ed2d46d364e867ca7d9ce009d3af8c1718.js and /public/assets/application-5d8165dcf2b8902df77aed37c7ab6fee8c7d62844cb48ece6360d1fa4f079804.css.
Also, I did change config.assets.compile = true in /config/environments/production.rb. But nothing is changed.
If you want to run production mode in your local environment you should tell rails to serve assets itself by changing following
config.serve_static_files = true
In production environment itself you have to configure it using nginx - http://vladigleba.com/blog/2014/03/27/deploying-rails-apps-part-4-configuring-nginx/
and make sure you have reverse your earlier change before pushing it to production.
config.serve_static_files = false

stylesheet_link_tag 'application' - wrong number of arguments

I have a Rails 4 app which uses Slim:
# application.html.slim
= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true
It throws an exception:
wrong number of arguments (3 for 2)
(in /Users/me/project1/app/assets/stylesheets/foundation-components.sass)
Even when I delete the last parameter, the error remains. How do I fix it?
# app/assets/stylesheets/foundation-components.sass
#import settings
#import foundation
The file _settings.scss exists, the file foundation.scssc only in cache.
you need to put your import files in quote #import 'settings';

Rails 4 is adding controller name to the asset url which causes 404 error

I am wondering why Rails 4 is adding the controller name to the asset request when I click on a controller specific page. For example, instead of this url (which is accurate): /assets/jqBootstrapValidation.js
It's generating this url (causing a 404 error): /controllername/assets/jqBootstrapValidation.js
It's happening for all js and css files I have manually added to the app/assets directories. Seems like the asset request is getting confused and adding the current directory name to the request but I don't know how to fix it (the assets are in the correct directory).
How do I remove the controller name from the asset request? Any advice would be appreciated.
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

Resources