How can I remove a line in one file on Rails? - ruby-on-rails

I am trying to remove a line in one file on Rails. I found a method gsub_file, but it was an undefined method in Rails 4. It's like reverse method of insert_into_file of thor.
e.g.) app/assets/stylesheets/application.css
Before
*= require_tree . <- Needs to be removed!
*= require_self
After
*= require_self
This should be performed in Rails Application Template.

If you are implementing an application template (for use with the rails new myapp -m option), try using the Thor gsub_file, like this:
gsub_file 'app/assets/stylesheets/application.css', /*= require_tree .\n/, ""
Take a look at the rails_apps_composer gem if you'd like to see lots of file manipulation using Thor.

Related

#= require not working after upgrade to Rails 5

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.

Sprockets dependencies based on ENV vars

I want to be able to use an environment variable to swap out a site-specific stylesheet with sass variables to define colors for an instance of a Rails app. I can't seem to figure out how to use ruby code inside a sprockets directive to define the dependency.
What seems like it should work:
// stylesheets/application.css
/*
*= require "#{ENV['SITE_STYLESHEET']}"
*= require core
*= require profile
*/
Where ENV['SITE_STYLESHEET'] = my_stylesheet and stylesheets/my_stylesheet.scss exists.`
The error I receive is: couldn't find file '#{ENV['SITE_STYLESHEET']}'
Is there any way to use ruby inside a directive?
This might not be the most eloquent way to do it, but I think you could do something like this.
Create initializers/assets.rb
In this file
Rails.application.config.assets.precompile += %w( ENV['SITE_STYLESHEET'] )

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.

Rails Admin: add javascript library to custom action

I'm building a custom action for rails admin that includes a custom view.
I want to include a local copy of sparkline.js but I can't figure out a way to do this.
I tried to add the sparkline.js to the /vendor/assets/javascripts/actions/action_name directory but it is not loaded by rails admin
Is there any other way to get this file loaded
I did this by putting the external library into the app/assets/javascripts/rails_admin/custom directory and adding a 'require' statement to the rails_admin ui.js file.
i.e.
// in app/assets/javascripts/rails_admin/custom/ui.js
//= require ./sparkline.js
You can do this with coffeescript too:
# in app/assets/javascripts/rails_admin/custom/ui.js.coffee
#= require ./sparkline.js

Require Formtastic styles in Rails 3.1

Formtastic help says to add next lines in application.css:
# app/assets/stylesheets/application.css
*= require formtastic
*= require my_formtastic_changes
But what i'm gonna do when it is scss? Can't find it in search engines.
theres nothing left to do for you, place your my_formtastic_changes.scss in app/assets/stylesheets. Rails will automagically compile your scss file and add it to application.css

Resources