Bootstrap navbar not appearing for Ruby on Rails - ruby-on-rails

I have a ruby on rails application. in the meetings.scss, i have already done
#import 'bootstrap-sprockets';
#import 'bootstrap';
in my gem file i have already bundled installed the below:
gem 'jbuilder', '~> 2.7'
gem "simple_calendar", "~> 2.0"
gem 'bootstrap-sass', '~> 3.3.6'
gem 'bootstrap', '~> 4.5.0'
gem 'jquery-rails'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
However, in my index.html.erb, I tried to do
<p id="notice"><%= notice %></p>
<nav class="navbar navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
</nav>
<h1>Meetings</h1>
But nothing is happening in the nav bar
In app/javascript/packs, my application.js looks like this
require("#rails/ujs").start()
require("turbolinks").start()
require("#rails/activestorage").start()
require("channels")
// Uncomment to copy all static images under ../images to the output folder and reference
// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>)
// or the `imagePath` JavaScript helper below.
//
// const images = require.context('../images', true)
// const imagePath = (name) => images(name, true)
Any reason why?

The preferred way to install Bootstrap (or any assets for that matter) in Rails 6 is with yarn. Here are the steps to get it to work:
1. In your terminal
yarn add bootstrap query popper.js
2. In config/webpack/environment.js
const webpack = require(‘webpack’)
environment.plugins.append(‘Provide’, new webpack.ProvidePlugin({
$: ‘jquery’,
jquery: ‘jQuery’,
Popper: [‘popper.js’, ‘default’]
})
3. app/javascript/packs/application.js
require(‘bootstrap/dist/js/bootstrap’)
4. app/assets/stylesheets/application.css
#import bootstrap/scss/bootstrap
5. app/assets/stylesheets/application.css
change to app/assets/stylesheets/application.scss

If you are using Rails 5
Rename your app/assets/stylesheets/application.css to app/assets/stylesheets/application.scss then put:
#import "bootstrap-sprockets";
#import "bootstrap";
Then, remove all the *= require_self and *= require_tree . statements from the sass file. Instead, use #import to import Sass files.
Require Bootstrap Javascripts in app/assets/javascripts/application.js:
//= require jquery
//= require bootstrap-sprockets
Source: https://github.com/twbs/bootstrap-sass
If you Rails 6 with Webpack you can follow these steps:
https://medium.com/#guilhermepejon/how-to-install-bootstrap-4-3-in-a-rails-6-app-using-webpack-9eae7a6e2832

Related

How should I require turbolinks in `application.js` in my Rails 5 application

I am building a relatively new Rails 5 application. I've been using the bootsy gem and I've been experiencing a problem where the wysiwyg will not show on the page until after a page refresh. After playing around for a while, I discovered that if I removed turbolinks from my application.js, the wysiwyg appears first time ( without a page refresh ). Can anyone tell me how I should be requiring turbolinks into my application.js. Thanks in advance.
application.js:
//= require jquery
//= require jquery_ujs
//= require jquery.turbolinks
//= require bootstrap-sprockets
//= require bootsy
//= require turbolinks
//= require_tree .
application.scss:
#import "bootstrap-sprockets";
#import "bootstrap";
#import "bootsy";
#import "articles";
gemfile:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '5.0.0'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.15'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
gem 'jquery-turbolinks'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'simple_form'
gem 'bootstrap-sass'
gem 'bootsy'
I'm using simple_form in combination with bootsy so my form looks something like this:
<%= simple_form_for #article, url: action_path do |f| %>
<%= f.input :title %>
<%= f.input :body, label: "Article Body", as: :bootsy %>
<%= f.submit %>
<% end %>
You need to use the turbolinks:load event like this :
$(document).on('turbolinks:load', ready)
function ready() {
//your code here
}
Your require is fine.
That's it.
So after a bit of playing around I found a solution. I added document.addEventListener('turbolinks:load', Bootsy.init); to my application.js and this solved the issue.

Can't get bootstrap working

I've been trying to connect bootstrap framework to my project by following this documentation and I can't set it up for some reason.
Why is it?
assets/stylesheets/application.scss
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any styles
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
* file per style scope.
*
#import "bootstrap-sprockets";
#import "bootstrap";
*
*= require bootstrap3-editable/bootstrap-editable
*= require_tree .
*= require_self
*/
assets/javascripts/application.js
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap
//= require bootstrap3-editable/bootstrap-editable
//= require_tree .
views/layouts/application.html.erb
<!DOCTYPE html>
<html>
<head>
<title>Greenbull</title>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<div id="container">
<div id="user_header">
<% if current_user %>
Logged in as <%= current_user.full_name %>
<%= link_to "Log Out", logout_path("current"), method: 'delete' %><br />
<p><%= link_to "My tasks", user_tasks_path(current_user) %>
<% else %>
<%= link_to "Sign Up", signup_path %> or
<%= link_to 'Log In', login_path %>
<% end %>
</div>
<% flash.each do |message_type, message| %>
<div class="flash-<%= message_type %>"><%= message %></div>
<% end %>
<%= yield %>
</body>
</html>
Gemfile
source 'https://rubygems.org'
group :development, :test do
gem 'rspec-rails', '~> 3.0'
end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.3'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'bcrypt'
gem 'pry'
gem 'pg'
gem 'bootstrap-x-editable-rails'
gem 'bootstrap-sass'
end
I have installed bootstrap-sass 3.3.5.1, sass-rails 5.0.3.
Any help?
Read the instructions of the bootstrap-sass gem and follow their conventions https://github.com/twbs/bootstrap-sass.
Import Bootstrap styles in app/assets/stylesheets/application.scss:
// "bootstrap-sprockets" must be imported before "bootstrap" and "bootstrap/variables"
#import "bootstrap-sprockets";
#import "bootstrap";

Rails 4: Not able to require Bootstrap

I am following Tuts+ tutorial on "Building a Polling Application in Rails". I have followed along with every foundational step to configure the app.
With the 'bootsrap-sass' gem installed, he simply adds the *= require bootstrap to the application.css file and instantly sees the changed results on his server. However, I receive the following error..
localhost:3000
Sprockets::FileNotFound in Polls#index
couldn't find file 'bootstrap'
(in /RAILS/tutspolls/app/assets/stylesheets/application.css:13)
Extracted source (around line #5):
<html>
<head>
3 <title>Tutspolls</title>
4 <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
5 <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
6 <%= csrf_meta_tags %>
</head>
This is what my files are looking like..
Gemfile.rb
source 'https://rubygems.org'
gem 'rails', '4.1.6'
gem 'sqlite3'
gem 'sass-rails', '~> 4.0.5'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
gem 'haml-rails'
gem 'pry-rails'
gem 'bootstrap-sass'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '~> 0.4.0', group: :doc
gem 'spring', group: :development
group :test do
gem 'factory_girl_rails'
gem 'minitest-rails'
end
stylesheets/application.css
*= require bootstrap
*= require_tree .
*= require_self
*/
..and this was added to config/application.rb
config.sass.preferred_syntax = :sass
Through a google search as well as similar questions on Stackoverflow, I find similar issues with answers that are not working for me. My guess is that other it's a version issue or simply my lack of understanding between the asset pipeline and gem architecture.
According to readme you need:
1) Remove old application.css and create application.css.scss
2) In new application.css.scss include this lines:
#import "bootstrap-sprockets";
#import "bootstrap";
and #import "bootstrap-sprockets" must be imported before #import "bootstrap".
I see that you mentioned stylesheets/application.rb file. What's this? Why do you use sass syntax in .rb file? Why do you use *= instead of #import? Where's importing of bootstrap-sprockets? Why do you skip it?
After looking over the versions of Bootstrap, I realized the tutorial is slightly older, so I switched from the defaulted bootstrap-sass which is version '~> 3.2.0' to ~> 3.1.1 and it works great.
Apparently the latest version of bootstrap-sass does not support *= require bootstrap and rather uses the #import which is current in the readme file. With the earlier version, I do not need to use #import 'bootstrap-sprockets' when utilizing*=`.

Rails 4 + bootstrap set up assets

I am trying to setup bootstrap on Rails4 using bootstrap-sass and I am getting this famous error:
Sprockets::FileNotFound - couldn't find file 'bootstrap'
(in app/assets/javascripts/application.js:16):
I have tried following:
twitter/bootstrap in application.js
gem 'bootstrap-sass', '~> 3.1.0' is outside group assets
Also tried bunch of other things on internet
I have spend lot of time taking different suggestions from other posts. How do I systematically debug this , how to setup bootstrap-sass ?
p.s:
Also been trying to get twitter-bootstrap-rails working with no luck.
Here are some files
application.js
//= require jquery
//= require jquery_ujs
//= require js-routes
//= require bootstrap
//= require_tree .
//= require bootstrap-slider
application.css.scss
*= require jquery.ui.core
*= require jquery.ui.theme
*= require_self
*= require bootstrap-slider
*= require_tree .
*= stub active_admin
*/
#import "bootstrap";
Gemfile
source 'https://rubygems.org'
ruby '2.0.0'
gem 'rails', '4.0.0'
gem 'sass-rails'
gem 'coffee-rails', git: 'git://github.com/rails/coffee-rails.git'
gem 'uglifier', '>= 1.0.3'
gem 'jquery-ui-rails'
gem 'font-awesome-sass'
gem 'less-rails'
gem 'therubyracer', :platform=>:ruby
#gem 'twitter-bootstrap-rails'
gem 'jquery-rails'
#gem 'jquery_mobile_rails'
gem 'js-routes'
gem 'cancan'
gem 'devise'
gem 'figaro'
gem 'haml-rails'
gem 'pg'
gem 'rolify'
gem 'sendgrid'
gem 'simple_form'
gem 'thin'
gem 'rake'
#To use db for storing cookies instead cookie-store
gem 'activerecord-session_store', github: 'rails/activerecord-session_store'
group :development do
gem 'better_errors'
#gem 'binding_of_caller', :platforms=>[:mri_19, :rbx]
#Commenting out platforms part, because may be that's stopping this to be used on the dev machine'
gem 'binding_of_caller'
gem 'guard-bundler'
gem 'guard-rails'
gem 'guard-rspec'
gem 'html2haml'
gem 'quiet_assets'
gem 'rb-fchange', :require=>false
gem 'rb-fsevent', :require=>false
gem 'rb-inotify', :require=>false
# Required with Rails panel chrome extension. This Gem should come after better_errors gem
gem 'meta_request'
end
group :development, :test do
gem 'factory_girl_rails'
gem 'rspec-rails'
gem 'pry-byebug'
gem 'pry-stack_explorer'
gem 'pry-rails'
gem 'pry-debugger'
end
group :test do
gem 'capybara'
gem 'database_cleaner'
gem 'email_spec'
end
group :production do
gem 'rails_12factor'
end
gem 'high_voltage'
#Linkedin Logins
gem "linkedin"
gem "omniauth"
gem "omniauth-linkedin"
gem "omniauth-facebook"
#postgres use hstore in active record
#gem 'activerecord-postgres-hstore'
gem 'state_machine'
gem "ruby-graphviz"
#payments
#gem 'stripe',:git => 'https://github.com/stripe/stripe-ruby'
#gem 'anjlab-bootstrap-rails', :require => 'bootstrap-rails', :github => 'anjlab/bootstrap-rails'
gem 'newrelic_rpm'
gem 'pgbackups-archive'
gem 'pg_search'
gem 'acts-as-taggable-on'
#gem 'activeadmin' , github: 'gregbell/active_admin'
gem "activeadmin", git: "https://github.com/gregbell/active_admin"
gem 'kaminari'
gem 'bootstrap-slider-rails'
gem 'bootstrap-sass', '~> 3.1.0'
Installing the Bootstrap Gem
1.) Add the Bootstrap Gem:
gem 'bootstrap-sass'
2.) Understand The Application.css File
app/assets/stylesheets/application.css
Application.css takes all the other files in your /stylesheets directory and combines them for when you run your app.
3.) Create a New SCSS File (app/assets/stylesheets/bootstrap_and_customization.css.scss)
#import 'bootstrap';
4.) Require Bootstrap's JavaScript
...
//= require jquery
//= require jquery_ujs
//= require bootstrap <--
//= require turbolinks
//= require_tree .
5.) Rails Assets
group :production do
gem 'rails_12factor'
end
6.) Bundle Install & Restart Server
Thats should be it !
On one of my projects (Rails 4.1) I had to include the bootstrap directly (not sass). Maybe it will give a hint on making the saas version work. So below are steps to include the bootstrap directly:
Download and and extract the bootstrap to
Rails.root/vendor/assets/bootstrap
Create file Rails.root/vendor/assets/javascripts/bootstrap.js file with contents like this:
//= require ../bootstrap/js/bootstrap.js
Now the most important part to make icons work. The font file urls have to be overridden for the Glyphicons Halflings font. Also asset_path helper has to be used. So create file Rails.root/vendor/assets/stylesheets/bootstrap.css.erb file with contents like this.
/*
=require ../bootstrap/css/bootstrap.css
*/
#font-face {
font-family: 'Glyphicons Halflings';
src: url("<%= asset_path 'glyphicons-halflings-regular.eot' %>");
src: url("<%= asset_path 'glyphicons-halflings-regular.eot?#iefix' %>") format('embedded-opentype'), url("<%= asset_path 'glyphicons-halflings-regular.woff2' %>") format('woff2'), url("<%= asset_url 'glyphicons-halflings-regular.woff' %>") format('woff'), url("<%= asset_path 'glyphicons-halflings-regular.ttf' %>") format('truetype'), url("<%= asset_path 'glyphicons-halflings-regular.svg#glyphicons_halflingsregular' %>") format('svg');
}
Now require bootstrap in the application.js and application.css
application.js
//= require bootstrap
application.css
*= require bootstrap
And finally let assets pipeline be aware of the fonts path and additional extensions to precompile. In the application.rb add:
config.assets.paths << Rails.root.join("vendor", "assets", "bootstrap", "fonts")
config.assets.precompile += %w( *.eot *.svg *.ttf *.woff *.woff2 )
After that RAILS_ENV=production rake assets:precompile should show that is has recognized font files and copied them to the public assets folder.
Then to test if it works in production enable serving static assets (in production.rb: config.serve_static_assets = true) and RAILS_ENV=production rails s
I ended up using bootstrap from a hosted CDN
%link{href: "//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css", rel: "stylesheet"}/
%link{href: "//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css" , rel: "stylesheet"}/
%link{href: "//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css", rel: "stylesheet"}/
I had exactly the same error. The solution was to change in:
config/environments/production.rb
The line
config.serve_static_assets = false
to
config.serve_static_assets = true
I'm not exactly aware of what this line does, but my workteam had the same problem on a project, and they changed this line, and it worked.
Try this:
Open config/application.rb, add the following line:
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)
In your config.ru file, add
require 'bootstrap-sass'
Rename your application.css.scss to something else e.g. custom_bootstrap.css.scss. I have no idea why it makes a difference but it did in my case.
Also, from what I understand, by default bootstrap-sass does not require you to add
//require bootstrap
to your application.js. Apparently it breaks some kind of functionality.
i had the same issue :couldn't find file 'bootstrap-sprockets' with type 'application/javascript'
The only solution was to comment off in the Gemfile
gem 'sass-rails', '~> 5.0' and add instead
gem 'sass-rails', '>= 3.2'
hope this helps other members
The fix for me was two parts ... my correct list first ... but the list alone wasn't the fix!
gem 'sass-rails', '~> 5.0'
gem 'bootstrap-sass', '~> 3.3.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
Then I had a gemfile.Lock issue ... if anyone else can't get the files to load & doesn't want to use the CDN...check your gemfile.Lock & see what versions are compiled off the gemfile (one is input for us as dev's & the other is what is actually ran by the framework - it will cause issues if you try to modify gemfile.lock).
To resolve the gemfile lock...close the gemfile & gemfile.Lock
I had to first delete the items in question from the "gemfile".
I did a "gem install <name of gemfile>" I wanted for each of them.
Then open the gemfile & gemfile.Lock to check results. Note, you still have to do all the configuration bootstrap etc requires for each of these. It doesn't hurt to run a rails assets:precompile either.
Note: The rails assets:precompile is a rails 5 command.
add to Gemfile
gem 'font-awesome-rails'
do bundle install
add to application.css
*= require font-awesome
and, restart rails server.

jquery-ui, Foundation, and Rails 3.1 not playing well together

I have a Rails 3.1 app that uses the foundation-rails gem and the jquery-rails gem and everything seems to be working. I tried to add the jquery-ui-rails gem to the mix and ran bundle install and everything looks good. Here is the relevant part of my gemfile:
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
gem 'mysql2'
gem 'devise'
gem 'omniauth'
gem 'omniauth-facebook', '1.4.0'
gem 'omniauth-google-oauth2'
gem 'jquery-rails'
gem 'json'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.1.4'
gem 'coffee-rails', '~> 3.1.1'
gem 'uglifier', '>= 1.0.3'
gem 'foundation-rails'
gem 'jquery-ui-rails'
end
Here is application.js:
//= require jquery
//= require jquery_ujs
//= require jquery.ui.all
//= require foundation
//= require_tree .
$(function(){ $(document).foundation(); });
Here is application.css:
/*
* This is a manifest file that'll automatically include all the stylesheets available in this directory
* and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
* the top of the compiled file, but it's generally better to create a new file per style scope.
*= require_self
*= require foundation_and_overrides
*= require_tree .
*/
/*
*= require jquery.ui.all
*/
I have a page that includes the following code:
<ul id='sortable_list'>
<% #list.items.each do |item| %>
<li><%=item.name%></li>
<%end%>
</ul>
</div>
</div>
<script>
$(function() {
$( "#sortable_list" ).sortable();
$( "#sortable_list" ).disableSelection();
});
</script>
The list is not sortable when the page loads and I get a "Uncaught ReferenceError: $ is not defined". I'm at my wits end - help, please!
OK, so the issue was the current version of Foundation (5.0.2). The documentation says that its script tag should be placed right before the closing tag. I put my script_tag for application.js there and, apparently, jQuery likes to be loaded in the head. So I moved //= require foundation out of application.js, moved <%= javascript_include_tag "application" %> back to the head, and added <%= javascript_include_tag "foundation" %> by itself at the bottom of the document. I figure this will work until they fix the bug in 5.0.2 that requires that it be loaded at the end of the body.

Resources