Rails turbolink js issue one click dropdown js not load - ruby-on-rails

Rails js load issue Turbolinks gem.
Navbar Dropdown one click not open after page refresh or second click to open dropdown how to solve this isssue. and also kuse turbolinks gem and boostrap gem.
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo01" aria-controls="navbarTogglerDemo01" aria-expanded="true" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse">
<% if current_user.present? %>
<ul class="navbar-nav mr-auto mt-2 mt-lg-0">
<% Tabs::NAVBAR.each do|tab_key, tab|%>
<% if tab[:type].eql?('dropdown_tab') %>
<li class="nav-item dropdown <%= active_navbar(tab_key) %>">
<% if current_user.projects.present? %>
<%= link_to tab[:title], fetch_route(tab_key), class: 'nav-link dropdown-toggle', id: 'myTab', data: { toggle: "dropdown" }%>
<%else%>
<%end%>
<div class="dropdown-menu">
<% dropdown_options(tab_key).each do|option| %>
<%= link_to option[:title], option[:path], class:'dropdown-item' %>
<% end %>
</div>
</li>
<% else %>
<li class="nav-item <%= active_navbar(tab_key) %>">
<%= link_to tab[:title], fetch_route(tab_key), class: 'nav-link'%>
</li>
<% end %>
<% end %>
</ul>
<div class="dropdown">
Profile <b class="caret"></b>
<div class="dropdown-menu dropdown-menu-right">
<li><%= link_to " Profile", edit_user_registration_path, class: "dropdown-item" %></li>
<li><%= link_to " Sign Out", destroy_user_session_path, method: :delete, class: "dropdown-item" %></li>
</div>
</div>
<%end%>
</div>
</nav>

I think you need to reinitialize the elements that requires bootstrap js when the event turbolinks:load is fired, check this question Rails Bootstrap 4 with Turbolinks
It's about the tooltip js, but the same applies for any element that requires js initialization.
Something like:
$(document).on('turbolinks:load', function() {
$('.dropdown-toggle').dropdown()
})

May be you need Dropdown.parseDocument(element) or Bulma(element).dropdown() in turbolinks:load event when used #vizuaalog/bulmajs.
// global `bulmaOptions` object should be defined before BulmaJS has loaded
// disable autoParse to prevent repeated `turbolinks:load` event
window.bulmaOptions = {
autoParseDocument: false
}
// import Dropdown from '#vizuaalog/bulmajs/src/plugins/dropdown';
import Bulma from "#vizuaalog/bulmajs";
document.addEventListener("turbolinks:load", () => {
Bulma.parseDocument();
// Dropdown.parseDocument(document.getElementsByTagName('body')[0]);
Array.from(document.querySelectorAll('.dropdown')).forEach(element => {
Bulma(element).dropdown();
});
});
Note: bulmaOptions.autoParseDocument = false

Related

Rails 5.2 - Sub-menus don't keep opened (Turbolink ?)

I have this sidebar and I am having troubles to open sub-menus.
When I click on a sidebar-dropdown it opens and closes immediately. The more I click the less it opens, and then it doesn't open anymore. Unless I reload...
I tried to disable Turbolink in this menu by adding data-turbolinks="false"in the html. It partially works but this implies other troubles.
The sidebar-dropdowns open and close as expected and for some reason I can't explain after some click in the the main navbar they become unclickable unless I reload the page...
A #appears at the end the url when clcik fails.. like so: http://localhost:3000//categories/66# (it happends if turbolink is enabled or disabled"
There are no error at all in the browser console.
In My app I also use gem 'webpacker', '~> 3.5.5'
Any idea of what could be wrong? What direction to take to fix this?
<div class="page-wrapper chiller-theme toggled"> <!--data-turbolinks="false" is added in the div when turbolink is disabled -->
<nav id="sidebar" class="sidebar-wrapper">
<div class="sidebar-content">
<div class="sidebar-brand">
Menu pro
<div id="close-sidebar">
<i class="fas fa-times"></i>
</div>
</div>
<div class="sidebar-menu">
<ul>
<li class="sidebar-dropdown">
<a href="#">
<i class="fas fa-cart-arrow-down"></i>
<span>Orders</span>
</a>
<div class="sidebar-submenu">
<ul>
<li>
<%= link_to "Orders", orders_path, class: "list-item" %>
<%= link_to "New Orders", incoming_orders_path, class: "list-item" %>
</li>
</ul>
</div>
</li>
<li>
</li>
<li class="sidebar-dropdown">
<a href="#">
<i class="fas fa-truck"></i>
<span>Market</span>
</a>
<div class="sidebar-submenu">
<ul>
<li>
<%= link_to "Markets", market_path, class: "list-item" %>
</li>
</ul>
</div>
</li>
<li>
</li>
<li class="sidebar-dropdown">
<a href="#" id="suppliers">
<i class="fas fa-address-book"></i>
<span>Suppliers</span>
</a>
<div class="sidebar-submenu">
<ul>
<li>
<%= link_to "New Suppliers", new_supplier_path, class: "drop-item" %>
</li>
<li>
<%= link_to "Suppliers", suppliers_path, class: "drop-item" %>
</li>
</ul>
</div>
</li>
<li>
<li>
<%= link_to clients_customization_path, class: "list-item" do %>
<i class="fa fa-palette"></i> Customisation
<% end %>
</li>
</ul>
</div>
<!-- sidebar-menu -->
</div>
</nav>
</div>
and this is the js file:
// the below turbolink:load is commented when turbolink is disabled
$(document).on('turbolinks:load', function() {
// The below commented jQuery line is commented when turbolink is disabled and uncommented when trubolink is enabled
// jQuery(function ($) {
$(".sidebar-dropdown > a").click(function() {
$(".sidebar-submenu").slideUp(200);
if ($(this).parent().hasClass("active")) {
$(".sidebar-dropdown").removeClass("active");
$(this).parent().removeClass("active");
} else {
$(".sidebar-dropdown").removeClass("active");
$(this).next(".sidebar-submenu").slideDown(200);
$(this).parent().addClass("active");
}
});
$("#close-sidebar").click(function() {
$(".page-wrapper").removeClass("toggled");
});
$("#show-sidebar").click(function() {
$(".page-wrapper").addClass("toggled");
});
// });
});
UPDATE > I figured out that in the case of turbolink is disabled that sidebar-dropdowns become unclickable after I visit a show page (any show pages make sidebar fails)In my show page I use turbolink for bootstrap carrousel.
So the deal is to find HOW I can make turbolink working correctly everywhere
I think you need to prevent the actual behaviour of the link that toggles the drowpdown.
$(".sidebar-dropdown > a").click(function(e) {
e.preventDefault();
....the rest
At least that will prevent the '#' on the url.

Navbar is appearing on homepage but not on post page (newbie here)

so I'm a total newbie, please do bear with me.
I'm following a course online where the instructor shows navbar appearing on homepage root path but also on other pages like the posts page. I followed exactly his instructions, but for some reason the navbar which is a partial, is only appearing on the homepage but NOT on the posts page - the code for navbar is as follow and the code for the other pages are all on https://github.com/cheese1884/-mywebsite
Thanks, guys.
<div class='container'>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<%= link_to "LessonRoll", root_path, class: 'navbar-brand' %>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor03" aria-controls="navbarColor03" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarColor03">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<%= link_to 'Home', root_path, class: 'nav-link' %>
</li>
<li class="nav-item">
<%= link_to 'Posts', posts_path, class: 'nav-link' %>
</li>
<li class="nav-item">
<a class="nav-link" href="#">Pricing</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#">About</a>
</li>
<% if current_user %>
<li class="nav-item dropdown">
<a class = "nav-link dropdown-toggle" data-toggle="dropdown" href="#" id="download">Admin Menu<span class="caret"></span></a>
<div class="dropdown-menu" aria-labelledby="download">
<%= link_to "App Settings", app_setting_path(#app_setting), class: 'dropdown-item' %>
<div class="dropdown-divider"></div>
Link
</div>
</li>
<% end %>
</ul>
<ul class="nav navbar-nav ml-auto">
<% if !current_user %>
<li class="nav-item">
<%= link_to "Sign Up", new_user_registration_path, class: 'nav-link' %>
</li>
<li><%= link_to "Log In", new_user_session_path, class: 'nav-link' %>
</li>
<% else %>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" id="download"><%= current_user.username if current_user %> <span class="caret"></span></a>
<div class="dropdown-menu" aria-labelledby="download">
<%= link_to "Edit Profile", edit_user_registration_path, class: 'dropdown-item' %>
<div class="dropdown-divider"></div>
<%= link_to "Log Out", destroy_user_session_path, method: :delete, class: 'dropdown-item' %>
</div>
</li>
<% end %>
</ul>
</div>
</nav>
</div>
In your yeti.html.erb, you need to put the navbar outside the yield section:
<body>
<%= render partial: 'navbar' %> <!-- or code for navbar -->
<div>
<%= yield %>
</div>
</body>
Edit : multiple layouts explaination
Rails uses a simple convention to find the correct layout for your
request. If you have a controller called ProductsController, Rails
will see whether there is a layout for that controller at
layouts/products.html.erb. If it can't find a layout specific to your controller, it'll use the default layout at app/views/layouts/application.html.erb.
(source)
In your case you can add a custom layout such as yeti.html.erb and specify your PostsController to use this layout with layout 'yeti'
Why the navbar is appearing on homepage but not on post page (original question)
Because as i answered above, you need to add the navbar partial inside the yeti.html.erb layout same as you did in the application.html.erb layout.
Why the navbar design is different
Because in application layout you link the application.scss stylesheet which only imports bootstrap (here)
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
In the yeti layout, you link yeti/theme_manifest.scss as stylesheet which imports both "bootstrap" and the overriding "theme" (here)
<%= stylesheet_link_tag 'yeti/theme_manifest', media: 'all', 'data-turbolinks-track': 'reload' %>
I hope this will help you to see more clearly.

Website not functioning on all browsers

My website works well on my mac and iphone but in IE it doesn't. In IE some of the navbar features aren't right (i.e. layout is incorrect, searchbox doesn't have placeholder text, one of the navbar elements (favourites) looks completely different). I'm not sure where the problem lies
layout/application.html.erb is
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<%= analytics_init if Rails.env.production? %>
<title>Guidelines for Me</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<link href="../assets/bootstrap-responsive.css" rel="stylesheet">
<%= csrf_meta_tags %>
<script type="text/javascript">
function clearDefault(el) {
if (el.defaultValue==el.value) el.value = ""
}
function clearText(){
search = $('.search-query');
if (search.defaultValue==search.value)
search.value = ""
}
type="text/javascript">
$(document).ready(function() {
$('.collapse').collapse();
});
</script>
</head>
<body>
<div class="navbar navbar-inverse 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>
<% if Guideline.count % 100 ==0 %>
<%= link_to "#{Guideline.count}" " guidelinesforme - yay!", guidelines_path, :class => 'brand' %>
<% else %>
<%= link_to "#{Guideline.count}" " guidelinesforme!", guidelines_path, :class => 'brand' %>
<% end %>
<div class="nav-collapse in collapse">
<ul class="nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
List by...
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "...Topic", topics_path %></li>
<li><%= link_to "...Organisation", hospitals_path %></li>
<li><%= link_to "...Specialty", specialties_path %></li>
</li>
</ul>
<% if user_signed_in? %>
<li><%= link_to "Favourites", favourites_path %></li>
<% else %>
<li><%= link_to "Favourites", register_path %></li>
<% end %>
</ul>
<ul class="nav pull-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<% if user_signed_in? %>
<%= current_user.first_name %>
<% else %>
Log in or Sign up
<% end %>
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<% if user_signed_in? %>
<li ><%= link_to "Edit profile", edit_path %></li>
<li> <%= link_to "Log out", logout_path %></li>
<% else %>
<li><%= link_to "Log in", login_path %></li>
<li><%= link_to "Sign Up", register_path %></li>
<% end %>
<li><%= link_to "About us", about_path %></li>
</li>
</ul>
</li>
<li >
<%= form_tag guidelines_path, :class => 'navbar-search', :onSubmit=>"clearText(this)",:method => :get do %>
<%= text_field_tag :search, params[:search], :class => 'search-query input-small', :placeholder=>"Search", :ONFOCUS=>"clearDefault(this)" %> <% end %>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="container">
<% flash.each do |type, message| %>
<div class="alert <%= flash_class type %>">
<button class="close" data-dismiss="alert">x</button>
<%= message %>
</div>
<% end %>
</div>
<div class="container">
<%= yield %>
</div>
</body>
</html>
I've used twitter bootstrap.
I did add
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
before my but it doesn't seem to make a difference.
First of all Placeholder is not supported till IE9 . But you can use javascript pollyfills to enable the placeholder support, use http://www.modernizr.com/ for that purpose.

Rails Hiding a div based on Page using IF and ELSE statements

I have a home page where I want to display a div in a certain way that it will require to be in the top of the application view.
Div is classed as "HOME-BANNER"
I am attempting to create an IF ELSE statement to reflect hiding the div if it is not on the home page.
My question is how do I call the current page in order to identify it as the home view's index. Also would I end the code or do I place an else statement to continue the rest of the page?
<html>
<head>
<title>WEBSITE</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<% if current_page == "home#index" %>
<div class="HOME-BANNER">
<% end %>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
WEBSITE
<ul class="nav">
<li><%= link_to "All Jobs", jobs_path %></li>
<li><%= link_to "My Profile", tempers_path %></li>
</ul>
<div class="float-right">
<div class="btn-group open">
<a class="btn btn-primary" href="#"><i class="icon-user icon-white"></i></a>
<a class="btn btn-primary dropdown-toggle" data-toggle="dropdown" href="#"><span class="caret"></span></a>
<ul class="dropdown-menu">
<li><i class="icon-pencil"></i> My Profile</li>
<li class="divider"></li>
<li><i class="icon-th-large"></i> Dashboard</li>
<li><i class="icon-th-list"></i> My Posts</li>
<li><i class="icon-th-list"></i> My Searches</li>
<li><i class="icon-stop"></i> <%= link_to ' Sign out', destroy_user_session_path, :method => :delete %></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</div>
<div class="footer"></div>
</body>
</html>
You want to do this from your controller. Since this is a localized event, use the controller that specifically hosts this View and not your application controller.
def whatever_view_this_is
#home_banner = true
end
Then in your view :
<% if #home_banner %>
<div class="HOME-BANNER">
<% end %>

Move the mobile bootstrap links to the right side

By default the links in the dropdown for the mobile container are on the left (as shown below):
Is there a way to get them to show up on the right and not the left? I tried using text-align:right; but it's not showing up clean:
You can create your own class, for example .align-right and add it to the .nav-collapse container to align the links to the right. This way your changes won't affect the other elements that rely on that class on the bootstrap stylsheet. Try this:
.nav-collapse.align-right {
text-align:right;
}
Then you can do this:
<div class="nav-collapse align-right"> ..links.. </div>
And your links will be placed on the right side instead.
Demo: http://jsfiddle.net/PWFSX/
From Bootstrap documentation:
http://getbootstrap.com/components/#navbar
You have to use these classes "navbar-form navbar-left
I used them like that:
<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="span10"></span>
</button>
<%= link_to 'Home', root_path, class: 'navbar-brand' %>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<%= render 'layouts/navigation_links' %>
</ul>
<ul class='nav navbar-nav navbar-right'>
<% if user_signed_in? %>
<li><%= link_to 'Sign out', destroy_user_session_path, :method => :delete %></li>
<% else %>
<li><%= link_to 'Sing in', new_user_session_path %> </li>
<% end %>
</ul>
</div>
</div>

Resources