Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 - which order to put jQuery and bootstrap?

I'm trying to figure out how to load assets in my application.js.

I have these required files (among others) in my application.js.

//= require bootstrap-sprockets
//= require jquery
//= require jquery-ui
//= require jquery_ujs

My gem file has:

gem 'jquery-rails'
gem 'jquery-ui-rails'

When bootstrap-sprockets is before jQuery, then my bootstrap dropdown menu (below) works fine.

<div class="row" style="margin-top:30px">
    <div class="col-md-3 col-md-offset-8">
        <% if policy(@project).show? %>    

            <div class="btn-group">
              <button type="button" style="color: black" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                STATUS: <%= text_for_state(@project.current_state) %> <span class="caret"></span>
              </button>
              <ul class="dropdown-menu">
                 <% if policy(@project).request_preapproval? %>    
                    <%= link_to "REQUEST APPROVAL", request_preapproval_project_path(@project), method: :put, :style=>"padding-left:20px; padding-right: 10px" %>
                <% end %>
                <li role="separator" class="divider"></li>
                <% if policy(@project).publish? %>    
                    <%= link_to "PUBLISH", publish_project_path(@project),  method: :put, :style=>"padding-left:20px; padding-right: 10px" %>
                <% end %>
                <li role="separator" class="divider"></li>

                <!-- <a href="#" class='btn btn-primary'>REQUEST MORE INFORMATION</a> -->
                <% if policy(@project).update? %>

                    <%= link_to "EDIT PROJECT", edit_project_path(@project), :style=>"padding-left:20px; padding-right: 10px" %>
                <% end %>

                 <li role="separator" class="divider"></li>
                <% if policy(@project).edit? %>    
                    <%= link_to "REMOVE", edit_project_path(@project), :style=>"padding-left:20px; padding-right: 10px" %>
                <% end %>
              </ul>
            </div>


        <% end %>
    </div>    
</div

However, in that case, my tab show function on the following setup doesnt work. I can't click on the tabs. If I move jQuery above bootstrap-sprockets, it does work, but then the drop down menu, which otherwise works, stops working.

<div class="row">
                <div class="col-md-10 col-md-offset-1">
                    <div class="dp-tab-1">
                        <ul class="dp-tab-list row" id="myTab">
                            <li class="col-md-3 col-xs-6 active" >
                                <a href="#tab-guag-content-first">
                                    <span>MOTIVATION</span>
                                </a>
                            </li>
                            <li class="col-md-3 col-xs-6">
                                <a href="#tab-guag-content-second">
                                    <span>OBJECTIVES</span>
                                </a>
                            </li>
                            <li class="col-md-3 col-xs-6">
                                <a href="#tab-guag-content-third">
                                    <span>VISION</span>
                                </a>
                            </li>

                        </ul>

                        <div class="dp-tab-content tab-content">
                            <div class="tab-pane row fade in active" id="tab-guag-content-first">

                                <div class="col-md-12">
                                    <div class="tab-inner">

                                        <p class='medium-text'>
                                            <%= @project.motivation %>
                                        </p>
                                        <br/>
                                    </div>
                                </div>
                            </div>

                            <div class="tab-pane row fade" id="tab-guag-content-second">
                                <div class="col-md-12">
                                    <div class="tab-inner">
                                        <p class='medium-text'>
                                            <%= @project.objective %>
                                        </p>
                                        <br/>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <img class='wow fadeInUp img-responsive' src="images/iphone-mockup.png" alt=""/>
                                </div>
                            </div>

                            <div class="tab-pane row fade" id="tab-guag-content-third">

                                <div class="col-md-12">
                                    <div class="tab-inner">
                                        <div class='services-content'>
                                            <strong>Long Term Research Vision</strong>
                                            <p class='medium-text'>
                                                <%= @long_term %>
                                            </p>
                                        </div>
                                        <div class='services-content'>
                                            <strong>Immediate Challenge</strong>    
                                            <p class='medium-text'>
                                                <%= @immediate_challenge %>
                                            </p>
                                        </div>    
                                        <br/>
                                        <!-- <a class='btn btn-primary'>READ MORE</a> -->
                                    </div>
                                </div>
                            </div>

                        </div>
                    </div>
            <!-

When I try to render the page in the case when bootstrap is loaded before jQuery, I get this error in the chrome inspector console tab:

main.self-9711b4c….js?body=1:56 Uncaught TypeError: dp(...).tab is not a function(anonymous function) @ main.self-9711b4c….js?body=1:56dispatch @ jquery.self-660adc5….js?body=1:5227elemData.handle @ jquery.self-660adc5….js?body=1:4879

How can I solve this so that I can have both the drop down and the tabs working?

My entire application.js is:

//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require bootstrap-sprockets
//= require html.sortable
//= require disqus_rails
//= require moment
//= require bootstrap-datetimepicker
//= require pickers
//= require masonry.pkgd.min
//= require multipleFilterMasonry
//= require main
//= require hammer.min
//= require jquery.animate-enhanced.min
//= require jquery.countTo
//= require jquery.easing.1.3
//= require jquery.fitvids
//= require jquery.magnific-popup.min
//= require jquery.parallax-1.1.3
//= require jquery.properload
//= require jquery.shuffle.modernizr.min
//= require jquery.sudoSlider.min
//= require jquery.superslides.min
//= require rotaterator
//= require smoothscrolljs
//= require waypoints.min
//= require wow.min
//= require underscore
//= require gmaps/google
//= require markerclusterer
//= require infobox
//= require chosen-jquery
//= require cocoon
//= require imagesloaded.pkgd.min
//= require isotope.pkgd.min
//= require jquery.counterup.min
//= require jquery.pjax
//= require custom.js
//= require slider
//= require bootstrap-slider
//= require_tree .

NEXT ATTEMPT

I have tried all of the suggestions in this post: none of them work for me: https://github.com/twbs/bootstrap-sass/issues/714

like image 668
Mel Avatar asked Sep 03 '16 07:09

Mel


People also ask

Can I use jQuery with Bootstrap?

Bootstrap 5 is designed to be used without jQuery, but it's still possible to use our components with jQuery. If Bootstrap detects jQuery in the window object it'll add all of our components in jQuery's plugin system; this means you'll be able to do $('[data-bs-toggle="tooltip"]').

Should I use Bootstrap or jQuery?

Using Bootstrap you can develop a feature that you can work in with every size of the screen as it provides consistency across all the browsers and different devices, Whereas, JQuery UI is used for the development of an user interface based on HTML to make the websites more responsive on all the browsers.

What is jQuery 5 instead of Bootstrap?

The upcoming major version of Bootstrap, version 5, will no longer have jQuery as a dependency and will be replaced with vanilla JavaScript.


2 Answers

Works perfectly.

//= require jquery
//= require jquery-ui 
//= require jquery_ujs
//= require bootstrap-sprockets

You have defined bootstrap.js before the jquery. but in every application you should defined jquery at the top, so it would be load first.

like image 161
Prashant Ravi Darshan Avatar answered Sep 20 '22 07:09

Prashant Ravi Darshan


This worked for me: https://stackoverflow.com/a/22977412/2860931 It doesnt feel like an actual solution - but rather a work around. I'll take that as good enough for now. I've tried at least 30 different solutions posted in various places on SO.

like image 31
Mel Avatar answered Sep 18 '22 07:09

Mel