#= require not working after upgrade to Rails 5 - ruby-on-rails

I upgraded Rails 4.2.10 application to Rails 5. Solved errors with bundling and some deprecations. I am able to start the Rails Application, but when I try to load the Application, it fails with error, Invalid CSS after "#": expected id name, was "= require in the css file
I tried using #import, but it fails for external files in vendor/assets. Nevertheless, I want to understand why it isnt working anymore. In my config, I have the file in config.assets.precompile +=
In my index.html.erb,
<%= stylesheet_link_tag 'users' %>
In users.scss,
#= require "dashboard/dx"
Note: I am using sass-rails in my Gemfile
The error that I get,
Sass::SyntaxError in Dashboard::Users#index
Invalid CSS after "#": expected id name, was "= require "dash..."
Extracted source (around line #1):
#= require "dashboard/dx"

Hey I think the error is throwing because in your typical .scss file you would have to use either an #import "dashboard/dx" and#= requiremight only work in.css`

The problem with this line
#= require "dashboard/dx"
This syntax is not working with .scss file. Either You have to write like this
#import 'dashboard/dx'
Or rename the file with .css extension.
Note: You can also try to rename with .css.scss extension May be it works also sometimes.

Related

Rails 6 remote: true and multipart: true file uploads not working

Can you upload files in a Rails 6 form with remote: true?
My form has remote: true and multipart:true, and works without issue when I do not upload any files.
Once I try to upload a file, I get a 406 error, and in my logs I'm seeing:
ActionController::UnknownFormat (AppProxy::RegistriesController£update is missing a template for this request format and variant.
request.formats: ["text/html"]
It seems only when I try to upload an image, I'm getting this error, otherwise my update.js.erb is running fine when other fields are saved and no files are uploaded.
Edit: It appears that the uploads are in fact saving despite the error, but I'm still only seeing this error when the form has uploads.
I assumed remotipart wasn't going to work because it explicitly says it's for Rails 3 and 4, but alas, it did work for me.
add gem 'remotipart' in Gemfile
//= require jquery.remotipart
add this line in app/assets/javascripts/application.js (right after //= require jquery_ujs):

Assets pipeline and vendor assets. How do you add them to be precompiled in Rails 4?

This is incredibly frustrating.
I have added a few CSS files to my vendor assets, and I cannot seem to get them to get precompiled, or at least FOUND by my production server.
The files are found in something like this:
/vendor/assets/stylesheets/cssfw/style.css
/vendor/assets/stylesheets/cssfw/app.css
/vendor/assets/stylesheets/cssfw/plugins.css
/vendor/assets/stylesheets/cssfw/custom.css
And they're loaded like this on my application.css
*= require cssfw/style.css
And inside style.css
#import url(app.css);
#import url(plugins.css);
#import url(custom.css);
This works fine in my development environment (obviously), but all of those 4 CSS files cannot be found anywhere in the assets in production.
I'm obviously missing to include something, but what?
If I use something like this in production.rb:
config.assets.precompile += %w( *.css )
I get a barrage of errors, cause then it goes through every single stylesheet I have in the /vendor/ directory. if I try to be specific (such as specifying style.css), the same initial errors persist, and style is nowhere to be found.
How are they supposed to be included exactly? Preferably, I wouldn't want to include ALL the vendor assets files, cause there's only a handful that I'm actually using, but the folder contains around 20 or so that I haven't yet configured.
I'm using Rails 4.1.6 with Ruby 2.1.0, on Apache Phusion Passenger.
The trick was in changing #import to *= require, cause #import isn't following the assets pipeline. I'm still unclear of why that happens, but I changed this in style.css, which after all was actually being loaded all this time.
In my application.css:
*= require cssfw/style # /vendor/cssfw/style.css
And in the first part of style.css:
/*
*= require cssfw/app # /vendor/cssfw/app.css
*= require cssfw/plugins # /vendor/cssfw/plugins.css
*= require cssfw/custom # /vendor/cssfw/custom.css
*= require cssfw/plugins/animate # /vendor/cssfw/plugins/animate.css
*= require cssfw/plugins/box-shadows # /vendor/cssfw/plugins/box-shadows.css
*/
This would make Rails to look for them in the proper places in the vendor pipeline, although I'm being forced to practically include the entire path, minus cssfw. If I leave it out, it works fine in development, but production cannot find them.
I didn't move anything else to my application.rb, or production.rb, in case you're wondering. I think this may have a more elegant solution, but this works for now.
you should set in production.rb:
config.assets.compile = true
and in command line use:
rake assets:precompile
before launching it your server.

Select2-rails is not working with ActiveAdmin

I have difficulties integrating select2-rails with ActiveAdmin. I followed setup steps on
Select2-rails Github page: https://github.com/argerim/select2-rails and I added line:
//= require select2
to app/assets/javascripts/application.js and line:
*= require select2
to app/assets/stylesheets/application.css
so I assume when I have page in ActiveAdmin I should be able to add line:
$('#add_student_select').select2()
to active_admin.js.coffee
But its not working. In console I can see following error:
Uncaught TypeError: undefined is not a function
(anonymous function)
fire
self.fireWith
jQuery.extend.ready
completed
I also followed this StackOverflow question which recommends to add this line to active_admin.css.scss:
body.active_admin {
#import "select2";
}
But then I get following error:
File to import not found or unreadable: select2.
Do I integrate it correctly? I don't think that ActiveAdmin is able to get even access to the librabry.
If you're adding Select2 to the ActiveAdmin interface, you must add the javascript and styles to the ActiveAdmin assets:
# app/assets/javascripts/active_admin.js.coffee
#
#= require select2
#
# ...
And the stylesheets:
// app/assets/stylesheets/active_admin.css.scss
//
//= require select2
//
// ...
In the example you provided, Select2 would be available to the main Rails application, but not ActiveAdmin. ActiveAdmin uses its own javascript and stylesheet files.
I had met the same issue making select2 work with activeadmin, but instead I used a gem named activeadmin-select2. I had installed it according to the README, but I still got error "File to import not found or unreadable: select2.". It seems like that select2-rails had not been installed or not be accessible, however. So then I tried to add select2-rails to my Gemfile, and bundle, lastly, everything went well. You should checked your gem loading before you can make it work.

Issue with Javascript include in Rails app

I'm using Rails 3.2.8. When the app is deployed access the view that is including a javascript:
<%= javascript_include_tag "epiceditor" %>
Heroku fails with this log:
ActionView::Template::Error (/app/app/assets/javascripts/epiceditor.js.erb has already been required
I've checked some possible solutions, like checking for any reference that may trigger a circular dependency, or simply removing it in case it is being included somewhere else, which isn't. So, if I include it, I get this "has already been included error", if I don't , then the file isn't included at all.
My config/application.rg has this
config.assets.initialize_on_precompile = false
And applications.js has this:
//= require jquery
//= require jquery_ujs
//= require tabs
It might be important to note that the file the tag is referencing is "epiceditor.js.erb", since it has some embedded Rails code that I needed.
Thanks for your help
EDIT:
I believe this is a bug in Sprockets. If I update Rails to 3.2.9rc2, the error is now this:
ActionView::Template::Error (Asset logical path has no extension: epiceditor/.js
but of course the extension in epiceditor is epiceditor.js.erb, and I've tried being explicit about it in the javascript_include_tag as well.
I found the bug.
It turns out that inside the .js.erb file I'm calling
<% asset_path 'epiceditor/' %>
which should expand to the path where all the epiceditor file are placed, but instead is actually loading the file itself in recursive manner. This is expanding properly in the development environment but not in the production environment. Funny, right?
The reason for this is that is adding a digest. So I fixed the whole issue with this:
<%= asset_path 'epiceditor/', :digest => false %>
and now it does expand to the directory, and doesn't fall into the recursion trap.
Hope this saves some time for someone!

is it possible to replace MarkerClusterer with MarkerClustererPlus in the gem ‘gmaps4rails’?

I would like to continue using the gem ‘gmaps4rails’ but use MarkerClustererPlus instead of MarkClusterer ... is this possible ?
Since the syntax seems to be the same, you should only disable the auto inclusion of the js files:
<%= gmaps( data_hash, true)
Then add these files yourself in your page and include the MarkerClustererPlus instead of the drfault one.
In your Gemfile, add this line:
gem 'markerclustererplus-rails'
You can include it by adding the following to your javascript file:
//= require markerclusterer
reference - https://github.com/RogerE/markerclustererplus-rails

Resources