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

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.

Related

Bootstrap navbar not appearing for 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

Dont work datepicker from gem 'jquery-ui-rails'

Version Rails 5 beta 3. I am using gem jquery-ui-rails 5.0.5
gemfile
................................................
gem 'jquery-rails'
# 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'
gem 'jquery-ui-rails'
application.js:
//= require turbolinks
//= require jquery
//= require jquery-ui/datepicker
//= require jquery_ujs
//= require jquery.turbolinks
//= require cocoon
//= require jquery_nested_form
//= require jquery-fileupload/basic
application.css
/**
* application.css
*
*= require jquery-ui/datepicker
*= require_self
*= require_tree ./all
freelancers/edit.html.erb
<li class="form-fields__group form-fields__group_birthdate">
<label for="freelancer_birth_date">Birthday</label>
<div class= "field">
<%= f.text_field :birthday %>
</div>
</li>
Table name: freelancers
#
# id :integer not null, primary key
# first_name :string
# last_name :string
# rate :integer
# birthday :date
javascripts/freelancer.coffee
jQuery ->
$('#freelancer_birthday').datepicker()
I click on field birthday, but dont open datepicker. Nothing happens. How can solve this problem? Thank you

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 Accordion icons not showing in Rails

I'm new to jQuery UI, but I can't figure out why this isn't working.
I have a simple accordion, and I'm trying to get the icons to show up. Here is my CoffeeScript:
$ ->
$( '#accordion' ).accordion
collapsible: true
heightStyle: "content"
icons:
header: "ui-icon-triangle-1-e"
activeHeader: "ui-icon-triangle-1-s"
In my application.js file I'm already including the follwing:
//= require jquery
//= require jquery_ujs
//= require jquery.ui.all
EDIT 1:
Relevant part of Gemfile
gem 'rails', '3.2.12'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
gem 'jquery-ui-rails'
Ok, I have tried jquery ui accordion and everything is ok.
simple-jquery-ui-accordion rails 3.2.13
$ ->
$( '#accordion' ).accordion
collapsible: true
heightStyle: "content"
icons:
header: "ui-icon-arrowthick-1-e"
activeHeader: "ui-icon-arrowthick-1-s"
I think the issue on your views or you forgot to include *= require jquery.ui.all on application.css
*= require_self
*= require jquery.ui.all
*= require_tree .
Make sure everything your script is correct
Is Header and activeHeader suppose to be a value for the icons key?
From the example: http://api.jqueryui.com/accordion/#option-icons
$( ".selector" ).accordion( "option", "icons", { "header": "ui-icon-plus", "activeHeader": "ui-icon-minus" } );
it looks like it 's a different argument.
which should look more like this
$(".selector").accordion "option", "icons",
header: "ui-icon-plus"
activeHeader: "ui-icon-minus"

Why are my bootstrap-sass rules overriden by the user agent rules?

I'm trying to use Twitter Bootstrap with the 'bootstrap-sass' gem but for some reason, the behavior is absolutely not right.
Judging by a submit button styled with class="btn" like so
<div class="actions btn">
<%= f.submit %>
</div>
The result is that there are the bootstrap styles, but they are overriden by the user-agent rules so that the result is the standard Chrome button that appears on top of the bootstrap one... Here is a link to what I get :
*http://hpics.li/9ddcb61
Also, without changing anything (all I did was try the suggestion in comments and put it back as it was), now things don't float around but are all vertical :
*http://hpics.li/f7605d0
This is what I get in Dev Tools :
*http://hpics.li/db602b0
This is the gem file
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'bootstrap-sass'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
end
I have a custom.css.scss file with this inside :
#import "bootstrap";
#import "_bootstrap-responsive.scss";
I also have tried adding this to the application.css.scss before the require_self and require_tree with no success
*= require _bootstrap
*= require _bootstrap-responsive
This is the js file in case it would be useful
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .
I have really no idea what is going on, moreover I did the railstutorial book and everything worked fine when I was on it, but on my own app, nothing works... :(
I'm not sure I gave all information but feel free to request!
It's doing exactly what you're telling it, namely creating a "Bootstrap-styled" button and placing a non-Bootstrap-styled submit button inside it.
Try simply adding the btn class to your submit button:
<%= f.submit , :class => "btn" %>
In bootstrap buttons and button styling requires the javascript part of bootstrap to be load also. You can do this by adding this line:
//= require bootstrap
to your app/assets/javascripts/application.js file above the line
//= require_tree

Resources