How to integrate a Wrap Bootstrap theme into an Rails application? - ruby-on-rails

I have bought a twitter bootstrap theme from wrapbootstrap.
I already have a functional rails application. Now, I want to design my application by integrating the bootstrap theme into my application.
I am new to this and I have no idea how to do it. After doing a lot of research on this, I found only a very few discussion regarding this issue. As for example I found this post: Implementing WrapBootstrap theme into Rails App
But, I am not totally sure how the assets from the theme will be applied to my application. I have copied all the assets under my project's app/assets/images, app/assets/javascripts and app/assets/stylesheets folders from the theme's corresponding folders. Then, I got several error when I tried to run my app locally. I deleted my application.css file, after that it started working. But, I can not see any design from the theme being applied yet. What should I do to make this theme work into my rails app?

First check this screencast:
http://railscasts.com/episodes/328-twitter-bootstrap-basics
then I would add a bootstrap gem like bootstrap-sass, then add the JS files through the gem by adding them to the manifest, something like this:
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .
then i would get the css files that you bought from wrapboostrap and place them in you assets/stylesheets folder, then add the necesary markup and clases to your app this is how ive done it before.
hope it helps
EDIT:
Markup:
Check the template you downloaded, lets start with the navbar for example
Code from template:
<header>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<div class="container">
<a class="brand" href="index.html">Gaia Business</a>
<div class="nav-collapse">
<ul class="nav">
<li class="active">Home</li>
<li>About</li>
<li>Service</li>
<li>FAQ</li>
<li>Contact</li>
<li class="dropdown">
Dropdown <b class="caret"></b>
<ul id="dropdown-menu" class="dropdown-menu">
<li>Dropdown 1</li>
<li>Dropdown 2</li>
<li>Dropdown 3</li>
<li class="divider"></li>
<li class="nav-header">Nav header</li>
<li>Dropdown 4</li>
<li>Dropdown 5</li>
</ul>
</li>
</ul>
</div><!-- /.nav-collapse -->
</div><!--/.container-->
</div><!-- /navbar-inner -->
</div>
</header><!--/header-->
Now you need to place yourself in your app, if the navbar shows in every view on your app, you should mention it on the layouts/application.html.erb something like this:
<!DOCTYPE html>
<html>
<head>
<title>Golden Green Chlorella</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= render :partial => 'layouts/navbar' %>
<%= yield %>
</body>
</html>
and last, do your navbar partial
_navbar.html.erb:
<header>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar glyph"></span>
<span class="icon-bar glyph"></span>
<span class="icon-bar glyph"></span>
</a>
<div class="container">
<%= link_to "Your app", root_path, :class => "brand" %>
<div class="nav-collapse">
<ul class="nav">
<li class=<%= current_page?(static_index_path) || current_page?(root_path) ? "active" : "" %> > <%= link_to (t "navbar.home"), root_path%></li>
<li class=<%= current_page?(static_know_path) ? "active" : "" %>> <%= link_to (t "navbar.know"), static_know_path%></li>
<li class=<%= current_page?(static_buy_path) ? "active" : "" %>> <%= link_to (t "navbar.buy"), static_buy_path%></li>
<li class=<%= current_page?(static_faq_path) ? "active" : "" %>> <%= link_to "FAQ", static_faq_path%></li>
<li class=<%= current_page?(static_contact_path) ? "active" : "" %>> <%= link_to (t "navbar.contact"), static_contact_path%></li>
<!-- <li class="active">Home</li> -->
</ul>
<ul class="nav pull-right">
<li><%= link_to "English", static_english_path%></li>
<li><%= link_to "EspaƱol", static_spanish_path%></li>
</ul>
</div><!-- /.nav-collapse -->
</div><!--/.container-->
</div><!-- /navbar-inner -->
</div>
</header><!--/header-->
That was only for the navbar, now you need to do the rest, add the markup your template shows you to do, with all your app, its not an easy job, but thats how its done.

make sure that while installing twitter bootstrap you should add following gem into your Gemfile under "group :assets"
gem 'therubyracer'
gem 'less-rails'
gem 'twitter-bootstrap-rails'
then run bundle command.
Now, the theme "file_name.css" (file_name could be any) that u have downloaded just add it into "stylesheets" folder under app->assests->stylesheets
then open your application.css file in same folder there you will see
*= require_tree.
replace this line with
*= require "file_name.css"
NOTE: Don't forget to re-compile your assets or simply delete the content of your tmp/cache folder.
save it and reboot your server. it will apply your new theme.

Related

Why is createPopper not a function even though I'm including Popper?

I have a navbar dropdown in my header and one in the body of my page. Currently, the navbar dropdown works. However, when I click on the icon that I've set to trigger the other dropdown, nothing happens.
Navbar dropdown:
<nav class="navbar navbar-expand-lg">
<div class="collapse navbar-collapse" id="desktop-header">
<ul class="navbar-nav">
<li class="nav-item dropdown pe-4">
<a class="nav-link dropdown-toggle text-dark fw-bold" href="#" role="button" data-bs-toggle="dropdown">Log</a>
<ul class="dropdown-menu">
<li>...</li>
</ul>
</li>
</ul>
</div>
</div>
The other dropdown outside of the header:
<div class="dropdown d-inline">
<a href="#" role="button" data-bs-toggle="dropdown">
<span class="mdi mdi-18px mdi-flag-outline text-dark me-2"></span>
</a>
<ul class="dropdown-menu">
<li>...</li>
<li>...</li>
</ul>
</div>
Here's the console error I get when trying to open the second dropdown:
bootstrap.source.js:2174 Uncaught TypeError: Popper__namespace.createPopper is not a function
at Dropdown._createPopper (bootstrap.source.js:2174:1)
at Dropdown.show (bootstrap.source.js:2063:1)
at Dropdown.toggle (bootstrap.source.js:2041:1)
at HTMLAnchorElement.<anonymous> (bootstrap.source.js:2400:1)
at HTMLDocument.handler (bootstrap.source.js:411:1)
Here is how I've included Bootstrap JS, taken straight from the v. 5.2.x docs:
<script
src="https://cdn.jsdelivr.net/npm/bootstrap#5.2.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-kenU1KFdBIe4zVF0s0G1M5b4hcpxyD9F7jL+jjXkk+Q2h455rYXK/7HAuoJl+0I4"
crossorigin="anonymous"
type="application/json">
</script>
I've also tried including Popper and Bootstrap separately, again per the official docs.
Further, when I don't include type="application/json" the dropdown in the body (outside of the navbar) works but the navbar dropdown doesn't. Conversely, when I include type="application/json" the navbar dropdown works but the other one doesn't.
How do I get both dropdowns to work correctly?
In my Rails layout file, I had two sections -- content_for :head_javascripts and content_for :body_javascripts.
It was in :body_javascripts that I was trying to include Popper and Bootstrap per the documentation's instructions.
By adding the following lines:
<%= javascript_include_tag 'railsapp' %>
<%= javascript_include_tag 'libs/bootstrapapp' %>
to the :head_javascripts section and deleting :body_javascripts, I got both dropdowns to work.
Any insight into why exactly this solution worked and not when I tried including Bootstrap in :body_javascripts is welcome.

Bootstrap Dropdown is not working in Ruby on Rails

I am new to rails development. I am creating one Rails app. In which I am using Bootstrap 4.0.0 beta. I have one navbar in particular div, (not on whole page). But problem is, dropdown is not working when I click on it. For that first I have to click on nav-brand first, then I can click on dropdown and it works. But it changes whole placings of navbar. and again if I click on nav-brand dropdown stops working.
I also tried putting it on whole page there is also same problem.
I have ruby-2.4.1, rails-5.1.3, bootstrap-4.0.0beta
Here is my /assets/stylesheets/application.css code
*= require bootstrap
And this is /assets/stylesheets/welcomeindex.css code
*= require bootstrap
*= require application
body, .jumbotron{
height: 100vh;
min-height: calc(100vh - 155px);
//overflow: hidden;
}
.jumbotron{
margin-bottom: 0px;
}
#media (max-width: 991px) {
.jumbotron{
height: 100%;
}
}
.footer {
position: fixed;
bottom: 0;
width: 100%;
height: 40px;
line-height: 40px;
background-color: #f5f5f5;
.container {
width: auto;
max-width: 680px;
padding: 0 15px;
}
}
This application.js file:
//= require jquery
//= require jquery_ujs
//= require popper
//= require turbolinks
//= require bootstrap-sprockets
//= require bootstrap
//= require_tree .
And this is welcomeindex.html.erb file:
<html>
<head>
<title> Fest Management </title>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= stylesheet_link_tag 'welcomeindex', media: 'all', 'data-turbolinks-track' => true %>
</head>
<body>
<div id="main-container" class="container-fluid">
<div class="row">
<div class="jumbotron col-lg-8 col-md-12">
</div>
<div class="jumbotron col-lg-4 col-md-12">
<nav class="navbar navbar-light bg-light">
<div class="navbar-header">
Brand
</div>
<div id="navbarCollapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a data-toggle="dropdown" class="dropdown-toggle" href="#">Messages <b class="caret"></b></a>
<ul class="dropdown-menu">
<li>Inbox</li>
<li>Drafts</li>
<li>Sent Items</li>
<li class="divider"></li>
<li>Trash</li>
</ul>
</li>
</ul>
</div>
</nav>
</div>
<footer class="footer navbar-fixed-bottom">
<div class="container">
<span class="text-muted">Place sticky footer content here.</span>
</div>
</footer>
</div>
</div>
</body>
</html>
So where am I missing? It would be great help.
EDIT
after solving the issue of dropdown click, I am getting the issue with CSS of navbar. when i expand dropdwon, rather than poping out of navabar area, it increases the height of navbar and shows whole list in it.
here is the edited index.html.erb file. Along with the edits suggested I have completely removed CSS references and still it giving the same problem for CSS.
<div id="main-container" class="container-fluid">
<div class="row">
<div class="jumbotron col-lg-8 col-md-12">
</div>
<div class="jumbotron col-lg-4 col-md-12">
<nav class="navbar navbar-light bg-light">
<div class="navbar-header">
Brand
</div>
<div id="navbarCollapse">
<ul class="nav navbar-nav">
<li class="dropdown show">
<a data-toggle="dropdown" class="dropdown-toggle" href="#" id="CategoryDD" aria-haspopup="true" aria-expanded="false">Messages <b class="caret"></b></a>
<ul class="dropdown-menu" aria-labelledby="CategoryDD">
<li>Inbox</li>
<li>Drafts</li>
<li>Sent Items</li>
<li class="divider"></li>
<li>Trash</li>
</ul>
</li>
</ul>
</div>
</nav>
</div>
<footer class="footer navbar-fixed-bottom">
<div class="container">
<span class="text-muted">Place sticky footer content here.</span>
</div>
</footer>
</div>
</div>
Adding to application.js
//= require jquery-ui
right after
//= require jquery
worked for me like a charm.
Hopefully, this will solve someone's issue too.
You shouldn't add body and html tags in a view. You also shouldn't add
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
All of this is usually already defined in application.html.erb which is the "skeleton" of your views.
Regarding your issue:
According to the documentation, you're missing an id on the a tag.
<div class="dropdown show">
<a class="btn btn-secondary dropdown-toggle" href="#" role="button" id="dropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Dropdown link
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</div>
Try removing any custom classes you've added and simply use this snippet. Should work out of the box if you leave all their elements.
If it still doesn't work, try running this in your console:
$('.dropdown-toggle').dropdown()
And let us know if there is an error.
You have added bootstrap but not jquery. Since bootstrap depends on jquery you must also add it. Before Rails 5, jquery used to be default in Rails app but Rails team removed it after Rails 5. If you want to use it then you should add this gem to your Gemfile. Here is the link https://github.com/rails/jquery-rails
Same issue, but solved with this kind of doing...
Did you overwrite bootstrap CDN or install any kind of bootstrap things like example of:
<!-- CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
If you're having this kind of in application.html.erb then kindly remove all kind of this...
and clear all cdn links and add again bootstap link
Thank you.

Navbar collapsing with bootstrap doesn't work

I want to have a navbar that collapses when I resize the screen, so I thought to use Bootstrap.
In the application.html.erb:
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="#">Some Store</a>
<div class="nav-collapse">
<ul class="nav">
<li><%= link_to "Browse Products" %></li>
<li><%= link_to "Price List" %></li>
<li><%= link_to "Contact Us" %></li>
<li><%= link_to "Cart" %></li>
</ul>
</div>
</div>
</div>
</div>
In the Gemfile I have gem 'bootstrap-sass'
In the application.css.scss I wrote #import 'bootstrap;'
In the application.js I have //= require bootstrap
I don't understand why It doesn't work, I just copied from the bootstrap site
Your imports are correct, but your current markup is not. It doesn't match the markup and classes that I see in some of the examples. I first attempted to build from your markup while using the sticky footer with fixed navbar example in the bootstrap docs as a guide to get the markup to display properly. Here's how I adjusted your markup:
<div class="navbar navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Some Store</a>
</div> <!-- end div class="navbar-header" -->
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li class="active"><%= link_to "Browse Products" %></li>
<li><%= link_to "Price List" %></li>
<li><%= link_to "Contact Us" %></li>
<li><%= link_to "Cart" %></li>
</ul>
</div> <!-- end div class="collapse navbar-collapse" -->
</div> <!-- end div class="container" -->
</div> <!-- end div class="navbar navbar-default navbar-fixed-top" -->
If you look closely, I swapped the locations of the divs with classes container and navbar-inner and changed the class to <div class="navbar-header"> that surrounds the collapse button. Then I had to change div surrounding the menu items to have class navbar-collapse instead of the nav-collapse that you had, otherwise the menu items disappear. In order for the button to work, I changed it from an <a> tag to a <button> tag as well as the data-target="navbar-collapse" attribute to correctly target the collapsible menu items. Once these changes were made, I had a functioning drop-down menu for window sizes < 768px.
And don't forget to add a style to your <body> tag for padding-top:51px; to guarantee your site content appears underneath the fixed navigation bar.
Hope that helps!
Chris
Please check your application.html.erb file in the header section add this
<meta charset="UTF-8">
<meta name=viewport content="width=device-width, initial-scale=1">

Bootstrap Togglable tabs rails

I want to use the Togglable Tabs from Twitter's Bootstrap on a Rails app, but I can't make it work.
Here is my app/views/pages/home.html.erb :
<ul class="nav nav-tabs" id="dashboard_products">
<li class="active"> 1 </li>
<li> 2 </li>
<li><a href="#given_products" > 3 </a></li>
<li> 4 </li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="owned_products"> 1 </div>
<div class="tab-pane" id="borrowed_products" > 2 </div>
<div class="tab-pane" id="given_products" > 3 </div>
<div class="tab-pane" id="requested_products" > 4 </div>
</div>
And here is my app/assets/javascripts/pages.js.coffee :
$ ->
$('#dashboard_products a').click = (e) ->
e.preventDefault()
$(this).tab('show')
And here my app/assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require_tree .
Where did I fail ?
You are missing the data-toggle tag.
Here is an example:
<div class="container-fluid">
<div class="row-fluid">
<div class="span8 well">
<ul class="nav nav-tabs">
<li class="active"> Home
</li>
<li>Profile
</li>
</ul>
<div class="tab-content">
<div class="tab-pane active fade in" id="home">home tab content</div>
<div class="tab-pane" id="profile">profile tab content</div>
</div>
</div>
</div>
</div>
DEMO
Check if you have included bootstrap js ...and also check you include coffee script file after you include bootstrap js. and coffee script include must also be after your html. as dom must create the element before init tabs..
The example here
http://getbootstrap.com/javascript/#tabs
doesn't have a data-target attribute.
<li role="presentation" class="active">Home</li>
But when I add it
<li role="presentation" class="active">Home</li>
It all worked.

Javascript and Css loading after the page loads

Before you go to website, I want you too look for this. You will notice when the website loads, the first second or two... it display the links in the navbar in a list format, and looks buggy... bc when it finishes loading it looks alright.
website: www.powerliftingbasics.com
This happens every time you refresh/load the page. I use ruby on rails with twitter bootstrap.
I can't figure it out.
here is my navbar code:
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="btn btn-navbar" data-target=".nav-collapse" data-toggle="collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<img src="assets/logosmall.png" alt="Small Logo">
<a class="brand navfont" href="/">Powerlifting Basics</a>
<div class="container nav-collapse">
<ul class="nav pull-right">
<li><%= link_to "Bench Press", "/benchpress" %></li>
<li><%= link_to "Deadlift", "/deadlift" %></li>
<li><%= link_to "Squat", "/squat" %></li>
<li><%= link_to "Equipment", "/equipment" %></li>
<li class="dropdown">
more<b class="caret"></b>
<ul class="dropdown-menu">
<li class="divider"></li>
<li><%= link_to "Subscribe", "/subscribe" %></li>
<li> Contact Us </li>
</ul>
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
</div>
In your layouts/application.html.erb
add application.css and application.js at the top
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>PowerLiftingBasics</title>
<%= stylesheet_link_tag "application" %>
<%= javascript_include_tag "application" %>
</head>
<body>
<%= yield %>
</body>
</html>
It happens cause you placed link to your css files at bottom of page. You should load it before page body. At least, put
<link href="/assets/application-65943938be14c000984bcb9478d30563.css" media="all" rel="stylesheet" type="text/css">
inside <head></head> tags.
The best practice is to place css and favicon links in head of page. Javascripts links place at bottom of body (before closing </body> tag).

Resources