where is jquery_rails javascript files located in rails 3.2? - ruby-on-rails

I've read the documentation on the asset pipeline. It says assets will be looked up in the 3 asset locations and compiled into the public folder, but I don't see any javascript in any of the 3 locations in the new rails project that I have just created. I have run bundle install and the jquery rails gems is installed. But grep for "jquery" gives no such file. Where is jquery located?

They're located within the gem itself.

Related

Active admin assets not compiling with webpacker

I am using webpacker for my asset pipeline in my rails app. I installed active admin as per the documentation. I moved the styleheet file and javascript file active_admin.js.coffee and active_admin.css from app/assets/stylesheets and app/assets/javascripts to vendor directory. To load these files from vendor directory I added following lines in my config/initializers/assets.rb
Rails.application.config.assets.precompile += %w( active_admin.js active_admin.scss )
But still rails is not able to find the file in my assets pipeline. It throws me error whenever I visit /admin path.
Sprockets::Rails::Helper::AssetNotFound in ActiveAdmin::Devise::Sessions#new
Showing /home/rabin/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/activeadmin-1.3.0/app/views/layouts/active_admin_logged_out.html.erb where line #9 raised:
The asset "active_admin.css" is not present in the asset pipeline.
I searched for entire stackoverflow but still can't figure out the problem.
ActiveAdmin on version 2.7.0 added Webpacker support, so after updating to this version (or above) according to docs ActiveAdmin will generate the needed files for you. Write:
rails g active_admin:install --use_webpacker
If you are not using Device add --skip-users after --use_webpacker
Unlike the usual generation ActiveAdmin will generate:
app/javascript/stylesheets/active_Admin.scss
app/javascript/packs/active_admin.js
This will download the needed js and CSS file with yarn, and update the Webpack to use jquery on all the pages (if you already set jquery, it will be worth removing the duplicate code)
If you updating from Sprockets don't forget to remove the previous js and CSS from assets or vendor folder.
It should have been inside vendor/assets directory.

Vendored assets in a Rails engine not loading when used as a Gem

Im writing an isolated Rails Engine which has it's own javascript in app/assets which in turn loads a bunch of dependencies that are kept in the engine's vendor/assets.
I've been using the dummy app in the test folder for development and everything has worked as I expect.
If I package the engine up as a gem and install it into a separate rails app, when I try to access the engine in a browser I get the Sprockets::FileNotFound exception couldn't find file.
If I fire up the console and have a look at Rails.application.config.assets.paths it includes mygem/app/assets but not mygem/vendor/assets.
This is where it gets weird. If I change the rails app's Gemfile and load the engine directly from a path, I don't have these problems. I can view my engine in browser without any Sprockets issues. Loading up the console and looking at Rails.application.config.assets.paths shows both path/to/mygem/app/assets AND path/to/mygem/vendor/assets.
I don't get this. Why would I get different behaviour if the engine is being loaded as a packaged gem or directly from a path?
Answering my own question. School-boy error, nothing to do with the asset pipeline, everything to do with adding the vendor path to the gemspec configuration.
s.files = Dir['{app,config,db,lib,vendor}/**/*', 'README.md', 'LICENSE.md']

Remove bootstrap media queries from rails app

Is there a way to remove just the boostrap media queries in my Rails app? I am unable to find the bootstrap.css file.
The bootstrap styles are located in the gem installation directory. You can find this directory by issuing the following command in your terminal and look for GEM_PATH:
gem env
Another way to find the gem path is by issuing
bundle show bootstrap
which will include the path of the gem installation.
Once you've located the path, copy the directory bootstrap from GEM_PATH/vendor/assets/stylesheets/ to your app/assets/stylesheets directory. I think the file you want to look at is app/assets/bootstrap/responsive.scss, at least this is where you want to start. Depending upon what you want to modify, modify the the content of this file.

How to manage Javascript dependencies with Bower in Rails apps?

The Javascript dependencies are growing in my Rails app and it's leading to some problems. For one, with so many it becomes difficult to track and update versions of different Javascript libraries. At first I tried turning them into gems, but then I have to manage those. Some are already gems (like backbone-rails) but I would like to have one consistent package manager.
So I'm looking into Bower, "a package manager for the web". Some blog posts from kaeff and from af83 have been helpful, but I still run into problems. I hope this question can lead to a variety of answers people can use to find the best solution for their projects.
I'd particularly like to see advice for managing assets through Bower in a Heroku deploy.
One way is using a version of Sprockets which allows your application.js to require packages defined by Bower. This feature was added in Sprockets 2.6, which Rails 3.x won't allow you to bundle due to a version restriction. To get that feature, you can bundle gem "sprockets", "2.2.2.backport1".
That will make Sprockets start looking for assets in vendor/assets/components too. The path is vendor/components because Bower packages could contain CSS or image assets too, not just Javascript. The way Sprockets knows what Javascript to include when you require the package is by reading the bower.json file for the main property, which says which files the pipeline should include. A gotcha here is that many Bower packages don't supply this property or supply main sources that assume RequireJS is available (e.g. d3). You can see what source main are defined with bower list --map.
When the main doesn't suit you, you can simply use Bower to manage a single remote JS file instead of a package. Josh Peek has a gist that demonstrates this. The latest version of Bower expects bower.json instead of component.json so I've updated the steps:
$ npm install -g bower
$ mkdir -p vendor/assets
$ cd vendor/assets/
$ curl https://raw.github.com/gist/3667224/component.json > bower.json
$ bower install
That particular bower.json loads jQuery. Then in your application.js you can simply //= require jquery and Sprockets will find it in your vendor/assets/components. To add new dependencies, just include them in your bower.json and run bower install again, for example:
{
"dependencies": {
"jquery": "http://code.jquery.com/jquery-1.8.1.js",
"d3": "http://cdnjs.cloudflare.com/ajax/libs/d3/3.0.8/d3.js"
}
}
If you want to manage your lib and vendor assets in one configuration file, you could use bower-rails, but there's not much point in using lib. That gem also provides some Rake tasks, but they don't do anything more than the basic Bower commands.
To my knowledge, there is no way yet to have Bower install as necessary during asset compilation. So if you're using a deploy environment like Heroku, you'll need to commit your vendor/assets/components directory and update it through the bower command.
It would be nice if could you require your whole Bower dependency set with one directive in application.js. A gist from kaeff illustrates how to create a require_bower_dependencies directive. There's no gem that does this for you yet. Until then, you have to declare each dependency in both bower.json and application.js.
Checkout Rails Assets project. Usage example:
source 'http://rubygems.org'
source 'https://rails-assets.org'
# gem 'rails-assets-BOWER_PACKAGE_NAME'
gem 'rails-assets-jquery', '~> 1.10.2'
gem 'rails-assets-sly', '~> 1.1.0'
gem 'rails-assets-raphael', '~> 2.1.0'
UPD (26.01.2016)
Future of Rails-Assets project is on discussion. Please check the thread and make your own decision: use it or find a workaround.
Just wrote up a guide for doing Rails+Bower+Heroku. Hope that helps.
I've been working through this issue for the last few days, and have settled on the following process.
Use the bower-rails gem. It provides a number of nice rake tasks that help integrate the use of bower with a rails application and the asset pipeline. I prefer to stick with the bower.json configuration instead of the ruby-based DSL that bower-rails provides. I stick with the default bower location that stores assets in vendor/assets/bower_components. Make sure to add the following to your application.rb.
config.assets.paths << Rails.root.join("vendor","assets","bower_components")
Once you have packages added to your bower.json, then you run the following:
rake bower:install
to install your javascript packages. You then reference the javacript in your application.js and application.css files.
As #andrew-hacking noted above rails and bower do not play nicely with javascript packages that have css and images, specifically css that references images. Bower-rails aids in fixing this issue with the provided rake task:
rake bower:resolve
It resolves relative asset paths in components by rewriting url references in component css files with the rails helper method to reference the asset in an asset pipeline friendly way. See BowerRails::Performer#resolve_asset_paths for more detail.
My preference is to check in all of the bower_components into our repository, so we run bower:install followed by bower:resolve. You then need to add the images referenced within the package to your config.assets.precompile list for your staging and production environments. For example here is the setup for adding the select2 javascript component to your rails project using bower and rails.
bower.json:
{
"name": "Acme",
"private": true,
"dependencies": {
"select2": "3.5.1"
}
}
app/assets/javascripts/application.js.coffee:
#= require select2/select2
app/assets/stylesheets/application.css.sass
//= require select2/select2
config/environments/staging.rb and config/environments/production.rb:
config.assets.precompile += ["select2/*.png",
"select2/*.gif"]
It's not perfect, but it gives you the dependency management of your javascript components, and it's better then trying to figure out what gems you need to pull in to get the desired javascript component enabled for the asset pipeline.
In case anyone else thinks using bower in your rails project is a good idea, think again.
Bower packages are simply not made to integrate with rails or sprockets.
What this means:
they won't typically specify a manifest index.js for including your javascript via sprockets require directives.
You will have to analyse and ferret around in the bower package and work out exactly what you need to require and from where (your require paths get kind of long)
bower packages don't uniformly specify a main key, and even if they do the current sprockets/bower support seems to ignore them
additional assets like fonts, images and templates won't be included in your production asset compilation (Rails 4 only includes app/assets/xxx)
Using config.assets.precompile patterns to try and 'get around' the problem is coarse grained and will pick up lots of cruft in your bower components directories
bower packages won't use sprocket url path helpers so any references to images and fonts within CSS/SCCS wont have the correct asset path. Good luck with that.
you will have to resolve all of the issues in EVERY project where you use bower packages
bundle install no longer works for your projects dependencies and you have to use another package manager as well
In contrast asset gems are simple to create and provide a uniform abstraction for pulling assets into your project. You have a chance to modify the original files and use asset helpers so things work well with your rails projects. There is a decent blog on creating asset gems, as well as a general railscast on creating gems too.
IMO It would be better for the rails community to maintain up to date asset gems than every project having to deal with the issues of a bower package that was never designed to plug into the rails asset pipeline.

Rails locations - where do I locate a gem

In Rails -
Where should I locate Gems? I downloaded bootstrap and it's working, as well as a sample Rails app, separately, but I want them to work together. There is a bootstrapped rails gem (http://rubygems.org/gems/bootstrapped-rails) which I downloaded, but I'm unsure as to where I should locate it. Under models?
And how do I make sure I am referring to it? I need to add something in controller as well?
Again, more an answer to the question in the title than to what was intended by the questioner but you can use
bundle show <gemname>
To locate the directory where a gem is installed.
As Dfr mentioned: https://github.com/seyhunak/twitter-bootstrap-rails
Twitter bootstrap isn't anything more than (mostly) a collection of css/js/image files.
Add this to your gemfile
gem "twitter-bootstrap-rails"
run
bundle install
run for simple css
rails generate bootstrap:install static
It should place relevant js and css files into your application.js and application.css files accordingly. (Read more about asset pipeline)
To get you started, in the gem's link under section - "Generating layouts and views", you can see rake tasks to generate sample layouts.
e.g.
rails g bootstrap:layout application fixed
You should now have a twitter-bootstraped application.html.erb file under views/layouts.
To answer the question in the title, you can locate your gems by running gem env in the console. That will give you the specific information about your "RubyGems Environment:" When you run gem install some_gem_name it will add this gem to your system.
However, what it sounds like your trying to do is add a gem to your app. If this is the case you add gems to a rails application's Gemfile.
So using your example, you'd locate your Gemfile and add the following:
gem "bootstrapped-rails", "~> 2.0.8.5"
Once that's done, you run bundle install in your terminal.
I find that a good resource for basic rails information can be found here: http://guides.rubyonrails.org/getting_started.html
The tutorial is short and it will give you a great starting point.

Resources