When I click on the "Account" link in the navbar, the dropdown menu is not appearing. Instead, a # is being added to the end of the URL, turning users/1 into users/1#.
A similar question regarding the dropdown issue has been posted here: ruby on rails chapter 8, 3rd Edition. About menu dropdown. I have experimented with various changes in my application.js file, as shown below. As far as I can tell, my sessions_controller.rb and sessions_helper.rb align with Hartl's instructions.
I am using Windows and Chrome browser for testing.
In the Chrome developer console, by enabling "show user agent shadow DOM," I am able to view the HTML structure of the dropdown menu, but no error messages are displayed.
My analysis suggests that there might be an issue either in the controller logic or due to an incompatibility between Bootstrap/Sass/Rails versions. While I did manage to get the dropdown functioning in Chapter 8 of Hartl's 2nd edition, I am hesitant to revert back to those older gem versions.
This is how my application.js file looks:
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require_tree .
I have also attempted variations like this:
//= require jquery
//= require bootstrap
//= require jquery_ujs
//= require turbolinks
//= require_tree .
Additionally, I have tried substituting bootstrap-sprockets for bootstrap.
Here is the content of my sessions_helper.rb file:
module SessionsHelper
def log_in(user)
session[:user_id] = user.id
end
def current_user
@current_user ||= User.find_by(id: session[:user_id])
end
def logged_in?
!current_user.nil?
end
end
I made an attempt to replace !current_user.nil? with current_user.nil? but it did not yield the desired outcome.
Code snippet from sessions_controller.rb:
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
log_in user
redirect_to user
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
end
end
Extract from Gemfile (relevant portion):
gem 'rails', '4.2.2'
gem 'bcrypt', '3.1.7'
gem 'pg', '~> 0.18.3'
gem 'bootstrap-sass', '3.2.0.0'
gem 'sass-rails', '5.0.2'
gem 'uglifier', '2.5.3'
gem 'coffee-rails', '4.1.0'
gem 'jquery-rails', '4.0.3'
gem 'turbolinks', '2.3.0'
gem 'jbuilder', '2.2.3'
Initial lines of Custom.css.scss file:
@import "bootstrap-sprockets";
@import "bootstrap";
Lastly, contents of _header.html.erb:
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<%= link_to "Birder's Life List App", root_path, id: "logo" %>
<nav>
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if logged_in? %>
<li><%= link_to "Users", '#' %></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account <b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Log out", logout_path, method: "delete" %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Log in", login_path %></li>
<% end %>
</ul>
</nav>
</div>
</header>
Your assistance would be greatly appreciated.