Rails 3.1 and Coffeescript: Require entire directory - ruby-on-rails

There is a require for individual files like so:
//= require controllers/documents
Is there an equivalent for requiring a directory?

Yep:
//= require_tree controllers
By the way, you tagged this as coffeescript, but your question uses JavaScript comment syntax... so for the record, in CoffeeScript you want to use #=, not //=.
Also, this is actually a Sprockets question, not a Rails question. Sprockets is a default in Rails 3.1, but you can use it independently as well. At any rate, the Sprockets page offers documentation aplenty on require, require_tree, and many other handy directives.

Related

How Do I Include Javascript and CSS from Older Gem in a Rails 6 App?

I'm just not sure how to add the javascript and CSS in when the gem I'm using hasn't been updated to use Webpack. I really like this tree view gem, the_sortable_tree, however, I haven't been able to get it to work in Rails 6 because it hasn't been updated. I'm happy to work on getting it up-to-date personally, but I'm not a master of how Ruby gems work--especially now with Webpack(er) in the mix. Anyhow, any ideas on what I need to do in order to leverage both the Rails 6 way of doing things and importing legacy gem code into my Rails 6 app?
In the documentation for the_sortable_tree, it says to add these to your application.js file:
//= require jquery
//= require jquery-ui
//= require jquery_ujs
But I have already added those using yarn so I think they should be good to go.
Then the docs say to add this code for the sortable tree UI:
//= require the_sortable_tree/jquery.ui.nestedSortable
//= require the_sortable_tree/sortable_ui/base
// with Turbolinks
$ ->
TheSortableTree.SortableUI.init()
// with Turbolinks
$(document).on "ready page:load", ->
TheSortableTree.SortableUI.init()
Is there a one-to-one translation for this? Or do the changes that are needed have to be implemented by the gem developer/creator?
How about the CSS? The docs say to add this to application.css:
*= require tree
*= require sortable_tree
*= require nested_options
*= the_sortable_tree/sortable_ui/base
So is there an equivalent in Rails 6? The paths here don't seem to be accessible.
Any help you can provide would be greatly appreciated. If I'm out of luck using this gem, so be it, but I just want to wrap my head around how this all works in general since Webpack(er) seems like such a departure from the old way of doing things that many gems seem to still employ.
Thanks.

Buggy bootstrap 3 navbar dropdowns and collapse

I'm using a bootstrap 3 navbar for a site I'm creating for fun. It looks great but I often run into a bug where the dropdowns won't expand or the collapse button won't work. I'm building it in Rails and added the cdn link to bootstrap.min.js at the bottom of my application.html.erb file.
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
Am I putting it in the wrong place? Thanks
Have you checked your console? Bootstrap requires JQuery, so it might not be working if you haven't included JQuery as well.
However, in a Rails app people usually use the gem bootstrap-sass to incorporate Bootstrap-3. This gem allows you to include bootstrap in the asset pipeline. It's easier to prioritize the order of your assets when they are all kept within the asset pipeline.
I would strongly recommend using the bootstrap-sass docs to include Bootstrap in your asset pipeline rather than including bootstrap via CDN in your html. The documentation has a step-by-step guide that tells you exactly how to include it, and it's also updated for Rails 5.
One of the benefits of Rails is that it allows you to keep your code very organized, so it's good to include Bootstrap in that organization.
Helpful Resources:
bootstrap-sass
integrating-rails-and-bootstrap
Update
With the bootstrap-sass gem your application.js file should look like this (plus any additional javascripts you might have added):
// app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap-sprockets
//= require_tree .
Most importantly, it needs these two lines in this order to work:
//= require jquery
//= require bootstrap-sprockets

Don't copy files into project for twitter-bootstrap-rails

Rails 4.2.4
Ruby 2.1.2
I am trying to use twitter-bootstrap-rails.
I would like to use it by the same way I am using jquery-rails
assets/applications.js
//= require jquery
In this case I don't need to copy any jquery.js or jquery.css files to my project because Rails fetches it for me form gem.
The different situation is with twitter-bootstrap-rails.
In the guide
The Twitter Bootstrap Rails gem can provide the Bootstrap stylesheets
in two ways.
The plain CSS way is how Bootstrap is provided on the official
website.
The Less way provides more customization options, like changing theme
colors, and provides useful Less mixins for your code, but requires
the Less gem and the Ruby Racer Javascript runtime (not available on
Microsoft Windows).
Seems the first way is more suitable for me. But in this case I should use the generator rails generate bootstrap:install static before to use any twitter-bootstrap .js or .css files. The generator fetches the files into assets folder of my project.
So I am looking for a way how to use twitter-bootstrap-rails .js and .css files in my project without copying them into my project folder. I just would like to add twitter-bootstrap-rails files just putting for instance line //= require bootstrap into application.js.
Thanks a lot.
Instead of using twitter-bootstap-rails, use bootstrap-sass.
In your application.js, add the following:
//= require bootstrap-sprockets
change application.css to application.scss and add:
#import "bootstrap-sprockets";
#import "bootstrap";
and you're good to go. Oh. Just please don't forget to restart your server.

Ruby on rails require jquery

Does Jquery have to be manually included in rails? Or is it a default? I see in the asset pipeline manifest:
require jquery
require jquery-ujs
It doesn't have to be included, but it will be included unless you remove it. Most applications will benefit from it being present, but if you don't use JavaScript in any of your views, you could always leave it out.

Where can I find a list of libraries that I can include with Sprockets directives?

I want to include the chosen library like my other js libraries in my Rails app like this in application.js:
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .
But I don't know if I just have to type //= require chosen and that's it.
So I tried to find a list of libraries that I can include with sprockets but I got nothing.
You need that javascript library packed as a gem. You're lucky, there is a gem.
Add gem chosen-rails to your Gemfile and run the bundle install-command. Afterwards, you can use //= require chosen-jquery in your application.js-file.
For further information please visit the github homepage of this gem.
To your question. Today it's quite common to pack such librarys as chosen is into a gem. Just search the name of the library you are looking for on http://rubygems.org/ or even google.
If no such gem exists, you could pack one by yourself (see this amazing article) or add the librarys by hand to your vendor-assets directory.

Resources