Showing users name on nav bar when logged in using ruby on rails - ruby-on-rails

I want to be able to replace Accounts tab on my navigation bar with the User's name when they logged in. How would i go about doing that. Would i have to use some sort of embedded ruby like <%= #user.name %>
heres the html
enter code here
<header class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", '#' %></li>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
Account
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", user_path(current_user) %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: 'delete' %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>

Try:
<%= current_user.name %>

Related

Why doesn't my dropdown menu expand to select the options within?

I'm building a review site using ruby on rails. When I code the drop-down menu, included in the code below, it won't expand.
I know from changing the code that the options are there and work, only the dropdown won't expand.
<body>
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<%= link_to "NDLabClub", root_path, class: "navbar-brand" %>
</div>
<ul class="nav navbar-nav">
<li><%= link_to "Sign Up", new_user_registration_path %></li>
<% if user_signed_in? %>
<li><%= link_to "Sign Out", destroy_user_session_path, method: :delete %></li>
<% else %>
<li><%= link_to "log In", new_user_session_path %></li>
<% end %>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
aria-expanded="false">Categories <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu">
<% Category.all.each do |category| %>
<li>
<%= link_to category.breed, dogs_path(category: category.breed), class: "link" %>
</li>
<% end %>
</ul>
</li>
<% if user_signed_in? %>
<li><%= link_to "Add Dog", new_dog_path%></li>
<% end %>
</ul>
</div>
</nav>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>

Bootstrap and ROR - Nav not horizontal

I'm trying to implement bootstrap into my ror app. For some reason my navs are not side by side, they are stacked on top.
Any suggestions?
<nav class="navbar navbar-default">
<div class="container">
<div class="navbar-header">
<%= link_to "Smart Health", root_path, class: "navbar-brand" %>
</div>
<ul class="nav navbar-nav">
<li><%= link_to "Register Doctor", sign_up_path %></li>
<% if signed_in? %>
<li><%= link_to "Sign out", sign_out_path, method: :delete %></li>
<% else %>
<li><%= link_to "Login", sign_in_path %></li>
<% end %>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Sort by Condition<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
</ul>
</li>
<% if signed_in? %>
<li><%= link_to "Your Patients", patients_path(user_id: current_user.id), class: "link"%></li>
<% end %>
<% if signed_in? %>
<li><%= link_to "Add Patient", new_patient_path %></li>
<% end %>
</ul>
</div>
</nav>
Try this if you use bootstrap 4
<nav class="navbar navbar-expand-md navbar-dark bg-dark">
<%= link_to "Smart Health", root_path, class: "navbar-brand" %>
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<%= link_to "Register Doctor", sign_up_path, class: "nav-link" %>
</li>
<% if signed_in? %>
<li class="nav-item"><%= link_to "Sign out", sign_out_path, method: :delete, class: "nav-link" %></li>
<% else %>
<li class="nav-item"><%= link_to "Login", sign_in_path, class: "nav-link" %></li>
<% end %>
</ul>
<ul class="navbar-nav ml-auto">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> Sort by Condition</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Action</a>
</div>
</li>
<% if signed_in? %>
<li class="nav-item"><%= link_to "Your Patients", patients_path(user_id: current_user.id), class: "nav-link" %></li>
<% else %>
<li class="nav-item"><%= link_to "Add Patient", new_patient_path, class: "nav-link" %></li>
<% end %>
</ul>
</nav>

Syntax error, unexpected keyword_ensure, expecting keyword_end

I currently have 3 loops im in my _header I have
if current_user.admin? && !current_user?(user)
Also
<% if logged_in? %>
And
<% else %>
It all works fine with no error if i take out
<% if current_user.admin? && !current_user?(user) %>
<li><%= link_to "Admin", users_path %></li>
</end>
When I paste it back in i get the error. I dont know whats wrong with this syntax.
Can anyone help?
<ul class="nav navbar-nav navbar-right">
<% if current_user.admin? && !current_user?(user) %>
<li><%= link_to "Admin", users_path %></li>
</end>
<% if logged_in? %>
<li><%= link_to "Dash", users_path %></li>
<ul class="nav navbar-nav">
<li class="dropdown">
Items<span class="caret"></span>
<ul class="dropdown-menu">
<li><%= link_to "New Item", new_item_path %></li>
<li><%= link_to "Edit Items", user_items_path %></li>
<li>Mass Upload Items</li>
<li>Upload Item Images</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav">
<li class="dropdown">
Profile<span class="caret"></span>
<ul class="dropdown-menu">
<li><%= link_to "View Profile", user_path(current_user) %></li>
<li><%= link_to "Edit Profile", edit_user_path(current_user) %></li>
</ul>
</li>
</ul>
<li><%= link_to "Messages", users_path %></li>
<li class="dropdown">
Settings<span class="caret"></span>
<ul class="dropdown-menu">
<li>Edit Money Back Policies</li>
<li>Edit Warranty Policies</li>
</ul>
</li>
<li><%= link_to "Log out", logout_path, method: "delete" %></li>
<% else %>
<li><%= link_to "Log In", login_path %></li>
<li>Sign Up</li>
<% end %>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</header>
You're not properly closing the if you pasted :
<% if current_user.admin? && !current_user?(user) %>
<li><%= link_to "Admin", users_path %></li>
</end>
You should try to replace that by :
<% if current_user.admin? && !current_user?(user) %>
<li><%= link_to "Admin", users_path %></li>
<% end %>
structure of if loop in ruby.your end tag of if loop is incorrect.see below
<%if%>
//condition here
<%end%>
can you try this out:
<% if logged_in? && !current_user?(user) && current_user.admin? %>
<li><%= link_to "Admin", users_path %></li>
</end>
Syntex error in closing of if block syntex.
1 <% if current_user.admin? && !current_user?(user) %>
2 <li><%= link_to "Admin", users_path %></li>
3 </end>
Error at line 3, replace line 3 ie </end> with <% end %>

No method error after adding a some HTML with embedded ruby

The code below works as it should, but after adding a dropdown navigation with some embedded ruby, I get a no method error.
NoMethodError in Static_pages#home
Showing D:/Ruby/sample_app/app/views/layouts/_header.html.erb where line #9 raised:
undefined method `find_by_token' for #Class:0x467a948
My _header.html.erb which gives no errors
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<%= link_to "Energy Battle", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<li><%= link_to "Log in", signin_path %></li>
</ul>
</nav>
</div>
</div>
</header>
This is the _header.html.erb with the code I added. Which gives me the error.
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<%= link_to "Energy Battle", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", '#' %></li>
<li id="fat-menu" 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 "Sign out", signout_path, method: "delete" %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>
It's telling you that there is a problem with your signed_in? method.
That's where you need to start checking.
Hope this helps

_header.html.erb account dropdown function does not work

I am following the ruby on rails tutorials from http://ruby.railstutorial.org .
The following is the _header.html.erb code, is the same as the tutorials. but the drop down function of account does not work.
<header class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<%= link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav pull-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Help", help_path %></li>
<% if signed_in? %>
<li><%= link_to "Users", users_path %></li>
<li id="fat-menu" 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", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li>
<%= link_to "Sign out", signout_path, method: "delete" %>
</li>
</ul>
</li>
<% else %>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
</ul>
</nav>
</div>
</div>
</header>
Without seeing more of the application it's hard to determine the exact problem, however based on what you have supplied I can suggest the following:
refer to http://twitter.github.com/bootstrap/javascript.html#dropdowns and ensure you are using the right markup nesting (looks like you are)
make sure you have included the javascript files within your application.js file (your manifest file). The docs for bootstrap here: http://twitter.github.com/bootstrap/components.html#navbar say that you need to have the dropdown JS file included to make the dropdown work properly.
Please let me know if any of this helps.

Resources