I am learning Bootstrap and am trying to implement it within a Ruby on Rails projects I have. I am using the bootstrap navbar and am trying to integrate that with rails. I have a search bar that I am trying to make responsive. I want the width of the search form to fill up the available space, and shrink/grow as the screen size changes. The navbar in bootstrap already has a lot of CSS styles applied to it and I feel like trying to add a feature like this to an existing navbar is difficult.
Here is the code for my form...
application.html.erb
<nav class="navbar navbar-default navbar-fixed-top" style="background-color: #228822;">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- overriding to HTTP GET because turbolinks causes issues with pagination infinite scroll -->
<%=link_to "The Pragmatic Bookshelf", store_index_path, :class => "navbar-brand", method: :get, style: "color:#bbffbb" %>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><%= link_to t('.questions'), store_index_path, style: "color:#bbffbb" %></li>
<li><%= link_to t('.news'), store_index_path, style: "color:#bbffbb" %></li>
<li><%= link_to t('.contact'), store_index_path, style: "color:#bbffbb" %></li>
</ul>
<form class="navbar-form navbar-left">
<div id="search_bar" >
<%= form_tag(store_index_path, method: :get) do %>
<div class="search_bar_text">
<%= text_field_tag :search, '', id: 'search_bar_text' %>
</div>
<div class="search_bar_image">
<%= image_submit_tag 'search.png', id: 'search_bar_image'%>
</div>
<% end %>
</div>
</form>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
Admin <span class="caret"></span>
<ul class="dropdown-menu">
<% if session[:user_id] %>
<li><%= link_to "Orders", orders_path%></li>
<li role="separator" class="divider"></li>
<li><%= link_to "Products", products_path%></li>
<li role="separator" class="divider"></li>
<li><%= link_to "Users", users_path%></li>
<li role="separator" class="divider"></li>
<li><%= link_to "Logout", logout_path, method: :delete%></li>
<% else %>
<li><%= link_to "Login", login_path%></li>
<% end %>
</ul>
</li>
<li class="dropdown">
Language <span class="caret"></span>
<ul class="dropdown-menu">
<% LANGUAGES.each do |language|%>
<li><%= link_to language[0].to_s, store_index_url(locale: language[1]) %></li>
<% end %>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
I got most of this code from an example in the bootstrap documentation and have been implementing some erb into it to integrate it with my Rails project. The form class="navbar-form navbar-left" is where the search bar starts. The behavior I want is for this to fill up the remaining space between the unordered lists to the right and left and adjust its size based on the current screen size.
I have experimented with display flex and flex-grow, floating, & overflow. Cant seem to get anything working. I wont include my CSS file since its only some font/color changes, most of the CSS is from bootstrap anyways.
Any help/guidance is appreciated :)
I'll ask the dumb question... do you have the following in your < head > section?
<meta name="viewport" content="width=device-width, initial-scale=1.0">
without it, Bootstrap won't be responsive.
Bootstrap includes a grid system. Maybe you can use this to define the width of the search field.
The grid system divides the width into 12 columns. So you could use for example:
.col-sm-12 .col-lg-4
You can play around with the values.
Related
This question already has answers here:
Bootstrap align navbar items to the right
(24 answers)
Closed 4 years ago.
My navbar wont go towards the right side. I set the div as you can see. However, div still is staying on the left side of the navbar.
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">BudLyfe</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarText" aria-controls="navbarText" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarText">
<ul class="nav navbar-nav navbar-right">
<% if (!user_signed_in?) %>
<li><%= link_to "Login", new_user_session_path %></li>
<li><%= link_to "Sign Up", new_user_registration_path %></li>
<% else %>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"
role="button" aria-haspopup="true" aria-expanded="false">
<%= current_user.email %> <span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Edit Profile", edit_user_registration_path %></li>
<li><%= link_to "Log out", destroy_user_session_path, method: :delete %></li>
</ul>
</li>
<% end %>
</ul>
</div><!--/.nav-collapse -->
</nav>
It looks like bootstrap 4 uses utility classes for navbar spacing instead of the bootstrap 3 version of navbar-right. In the future, be sure you are looking at the correct documentation for the specific version of bootstrap that you are using.
Using ml-auto in place of navbar-right pushes that list to the right side of the navbar. This ml-auto class stands for margin-left: auto; which is the individual css rule you would use to achieve this effect without bootstrap.
Example codepen here: https://codepen.io/anon/pen/QzBKPe
I have a problem with the content of my application page in rails,I basically cannot put any content after the <% yield %> tag.
My problem is that when I put a footer underneath the <% yield %> it shows up over the yield content, not bellow. I am sure there is a simple solution for this but I just cannot get it to work as I would like to.
I anyone have an idea of were the problem could be would be really helpful.
<body>
<nav class="navbar navbar-fixed-top" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<%= link_to (image_tag("logo.png", class:"food-image")), root_path%>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li class="sections"><%= link_to 'Dishes', posts_path %></li>
<li class="sections"><%= link_to 'Events', posts_path %></li>
<li class="sections"><%= link_to "About", about_path %></li>
<ul class="social-icons">
<li class="fa fa-twitter fa-2x"></li>
<li class="fa fa-facebook fa-2x"></li>
<li class="fa fa-instagram fa-2x"></li>
<li class="fa fa-pinterest fa-2x"></li>
</ul>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<% if current_page?(root_path) %>
<div class="image-header">
</div>
<% end %>
<div class="anchor">
</div>
<%= yield %>
<footer>
<div class="container-fluid" id="logs">
<div class="login-buttons">
<% if user_signed_in? %>
<button><%= link_to "New Post", new_post_path , class:"sing_in"%></button>
<button><%= link_to("Logout", destroy_user_session_path, :method => :delete, class:"sing_in") %></button>
<% else %>
<button><%= link_to("Login", new_user_session_path, class:"sing_in") %></button>
<% end %>
</div>
<p>lnjkbhjghfxgdzfxgchvjbknl</p>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
</div><!-- container-fluid-->
</footer>
Thanks in advance
This sounds like a CSS problem. If there's a float in the yield somewhere, you could need to clear it. Immediately after the yield and before footer, insert a spacer ...
<div style="clear:both"> </div>
Hopefully that puts you on the right track.
I have a project that i am working on. I have a navbar at the top and in the navbar i have a dropdown menu that does not work. I've been ignoring the problem for a while but now i need things in the dropdown menu. I have been slowly taking things out of my project trying to find the cause. I am now at a point where i have replaced my navbar, (located in my header file) with the bootstrap demo one.
This is my _header.html.erb (pretty much the same as bootstrap, except for the head tag)
<head>
<script type="text/javascript">
$('.dropdown-toggle').dropdown()
</script>
</head>
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<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="#">Brand</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active">Link</li>
<li>Link</li>
<li class="dropdown">
Dropdown <b class="caret"></b>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li class="divider"></li>
<li>Separated link</li>
<li class="divider"></li>
<li>One more separated link</li>
</ul>
</li>
</ul>
<form class="navbar-form navbar-left" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<ul class="nav navbar-nav navbar-right">
<li>Link</li>
<li class="dropdown">
Dropdown <b class="caret"></b>
<ul class="dropdown-menu">
<li>Action</li>
<li>Another action</li>
<li>Something else here</li>
<li class="divider"></li>
<li>Separated link</li>
</ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
This is my application.html.erb
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
<%= stylesheet_link_tag "application", media: "all","data-turbolinks-track" => true %>
<%= javascript_include_tag "application", "data-turbolinks-track" => true%>
<%= javascript_include_tag "http://localhost:9292/faye.js"%>
<%= javascript_include_tag "autocomplete-rails.js" %>
<%= include_gon %>
<%= csrf_meta_tags %>
<%= render 'layouts/shim' %>
</head>
<body>
<div class="container" style = "width: 100%; padding: 0px">
<%= render 'layouts/header' %>
</div>
</body>
</html>
The page loads but when i click on "dropdown" nothing happens. I'm stumped, does anyone have any ideas?? I have also been disecting my code looking for syntax errors and haven't found any. Since i am still getting the same error with bootstraps code it seems unlikely that a syntax error is the cause.
Just looked at your partial _header.hml.erb and noticed your have a head tag inside it. You should remove it.
If you look at your application.html.erb layout you already have a head tag there and you are rendering that _header.html.erb partial inside your body tag.
You have
<%= javascript_include_tag "application", "data-turbolinks-track" => true%>
which links your javascript to assets/javascript/application.js. If you need to write any javascript in your project then that is the place to write it or you could make any new js file inside assets/javascript and then require that file inside your application.js file
You should remove this
<head>
<script type="text/javascript">
$('.dropdown-toggle').dropdown()
</script>
</head>
from your partial and instead simply write in your application.js:
$(document).on("click",".dropdown-toggle",function(){
$(this).dropdown();
});
I have used on function used to incorporate for turbolinks
I made a Navbar with Bootstrap 3. My questions is how to have it be shaded when selected?
Everything works fine otherwise. I believe it required some JavaScript, but I don't know how to implement it.
This is my code:
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div style="font-family:Webdings;">
<%= link_to "lalala", root_path, class: 'navbar-brand' %>
</div>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav">
<li><%= link_to "laaa", lal_path %></li>
<li class="dropdown">
lala<b class="caret"></b>
<ul class="dropdown-menu">
<li><%= link_to "lala", laa_path %></li>
<li><%= link_to "lalala", lala_path %></li>
<li><%= link_to "lalala", lala_path %></li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
Follow #lalala
</li>
<li>
<div class="fb-like" data-href="lalala" data-width="600" data-layout="button_count" data-show-faces="true" data-send="false"></div>
</li>
<li><%= link_to "lala", contact_path %></li>
<li><%= link_to "lalalala", register_path %></li>
</ul>
</div><!-- /.navbar-collapse -->
</nav>
CSS3 is all you need! Go here: http://css3generator.com/. After you generate the shadow, gradient or text shadow you like, apply that css3 chunk of code to the element by adding :active selector. See here for :active selector: http://www.w3schools.com/cssref/sel_active.asp
NB: :active MUST come after :hover (if present) in the CSS definition in order to be effective!
I have an is_active? helper to define the active link in my nav bar. It works fine. But I have a clients page that has many clients in it (in a sidebar list), and when I click through each client, the page reloads and gets the url for the client id (/clientes/1, /clientes/2 ...). My is_active? only works for the first client (id=1). When I click the other clients the active class disappears.
my header view looks like this:
<nav class="navbar navbar-inverse navbar-default" role="navigation">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="container" id="container-header">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<%= link_to "Zetta Comunicação", root_path, class: "navbar-brand logo img-responsive" %>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="<%= is_active?(servicos_path) %>">
<%= link_to "A Empresa", servicos_path %>
</li>
<li class="<%= is_active? (cliente_unique_path(1)) %>">
<%= link_to "Clientes", cliente_unique_path(1) %>
</li>
<li class="<%= is_active?(new_contato_path) %>">
<%= link_to "Contato", new_contato_path %>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div>
</nav>
the helper looks like this
def is_active?(link_path)
if current_page?(link_path)
"active"
else
""
end
end
I know that I am calling the first id (id=1) in my header view. But I don't know how to make this call the other ids..
If I take the (1) of the cliente_unique_path it works perfect for the clients page, but I get the following error in my other pages:
No route matches {:action=>"cliente_unique", :controller=>"clientes"}
missing required keys: [:id]
Any ideas?
I would use jQuery in these situations. Like,
function activate(i){
("ul.nav li").removeClass("active");
("ul.nav li#" + i).addClass("active");
}
put that code in application.js.
give ids to all your li items.
and in every page call that function in a script tag passing the id of the li that you want to activate.
(typing from phone, so forgive the readability)