How to Render Current User into Nav-Bar Button - ruby-on-rails

Running:
ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux]
Rails 4.2.5
I want to render the current user name into the nav-bar button using the dropdown-menu class. I would like to replace "Account Info" with "Hi "current_user".
<li class="dropdown">
Account Info <span class="caret"></span>
<ul class="dropdown-menu">
<li> <%= link_to "Edit Profile", edit_user_registration_path, class: "fa fa-pencil-square-o" %></li>
<li> <%= link_to "Sign out", destroy_user_session_path, method: :delete, class: "fa fa-sign-out" %></li>
</ul>
<ul class = "nav pull-right">
</ul>
<% else %>
<!-- <%= link_to "Sign up", new_user_registration_path, class: "fa fa-sign-in" %> -->
<li><%= link_to "Sign in", new_user_session_path, class: "fa fa-sign-in" %></li>
<% end %>
</ul>
I'm using devise (3.5.3) for user authentication.

You'll just need to use an if statement to work out if the user is signed_in (I presume this method exists in Devise) and show a different button if the user is signed in:
<li class="dropdown">
<% if signed_in? %>
Hi, <%= current_user.user_name %> <span class="caret"></span>
<% else %>
Account Info <span class="caret"></span>
<% end %>
<ul class="dropdown-menu">
<li> <%= link_to "Edit Profile", edit_user_registration_path, class: "fa fa-pencil-square-o" %></li>
<li> <%= link_to "Sign out", destroy_user_session_path, method: :delete, class: "fa fa-sign-out" %></li>
</ul>
<ul class = "nav pull-right">
</ul>
<% else %>
<!-- <%= link_to "Sign up", new_user_registration_path, class: "fa fa-sign-in" %> -->
<li><%= link_to "Sign in", new_user_session_path, class: "fa fa-sign-in" %></li>
<% end %>
</ul>
You'll probably want to use a similar if, else, end statement to hide the sign_in/sign_up buttons when the current_user is signed_in too - as they won't want to see that.

This would suffice in your design simply.
<%- if user_signed_in? %>
<%= current_user.user_name %>
<% end %>
You have to first ask if the user exists. The reason why is because if you run .user_name on a user who is not logged in it is essentially running a method on a nil value. Which is a big no no.
Now if you still have an issue you have to make sure you have this in your controller:
before_filter :authenticate_user!

<%= content_tag :li, class: "dropdown" %>
<% if user_signed_in? %>
<%= link_to "Hi, #{current_user}", "#", data: {toggle: "dropdown"}, role: :button, aria: {haspopup: "true", expanded: "false"} %>
<%= content_tag :ul, class: "dropdown-menu" do %>
<%= content_tag :li, link_to("Edit Profile", edit_user_registration_path, class: "fa fa-pencil-square-o") %>
<%= content_tag :li, link_to("Sign out", destroy_user_session_path, method: :delete, class: "fa fa-sign-out") %>
<% end %>
<% else %>
<%= content_tag :li, link_to("Sign in", new_user_session_path, class: "fa fa-sign-in") %>
<% end %>
<% end %>
Refs:
content_tag

With some testing <%= current_user.first_name %> in Hi, <%= current_user.first_name %> <span class="caret"></span> seem to give me the outcome I was looking for. Thanks for your help and pointing me in the right direction. #Alain-Goldman.

Related

Chaning image-button to text-button using Devise login

I have it currently so someone can click on the login button and when somebody signs in it changes that to logout. How would I be able to make it so it sets the button as the user's avatar that they have uploaded (or default one) when they login and then when they logout it changes back to "sign in"?
<div class="dropdown">
<button class="button-login">
<% if user_signed_in? %>
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to "Login", new_user_session_path %>
<% end %>
</button>
<div class="dropdown-content">
<p>
<% if user_signed_in? %>
<%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %>
<% end %>
</div>
I believe you can do this by using a block:
<%= link_to destroy_user_session_path, method: :delete do %>
<%= image_tag("avatar.jpg", :alt => "user avatar image") %>
<% end %>

Why is my object printing to the page in rails?

When I run my rails server in chrome, at the end of the unordered list, the object hash is printing to the page. I am not sure why this is happening. Here is my show.html.erb -
<h1><%= current_user.email %>'s Cars</h1>
<ul>
<%= current_user.cars.each do |car| %>
<li><%= car.color %></li>
<li><%= car.make %></li>
<li><%= car.model %></li>
<li><%= car.year %></li>
<button class="btn btn-default"><%= link_to 'Edit', edit_car_path(car) %>
</button>
<button class="btn btn-default"><%= link_to 'Delete', car, :data => {:confirm
=> "You Sure?", :method => :delete} %></button>
<%end%>
</ul>
<% if user_signed_in? %>
<button class="btn btn-default"><%=link_to "Add a car",
new_user_car_path(current_user)%></button>
<%end%>
you are itreating that hash and printing it at the same time
remove the # from this line<%= current_user.cars.each do |car| %>
to following <% current_user.cars.each do |car| %>

Hiding a link from anonymous users

I'm using only devise gem for the user authorisation.
<ul class="actions">
<li>
<%= link_to 'New Item', new_item_path, class: 'new' %>
</li>
</ul>
How can I hide the New Item link from anonymous users?
user_signed_in? is predefined method provided by devise to check if user logged in or not.
<ul class="actions">
<% if user_signed_in? %>
<li>
<%= link_to 'New Item', new_item_path, class: 'new' %>
</li>
<% end %>
</ul>

Changing nav-bar links according to whether a user has created a profile or not (RoR)

I'm making a rails app and have created a simple navbar that works fine. I have set up devise and created a profile model which I would like users to create after they sign up, and (I believe) I have correctly made the necessary associations.
As a new user signs in for the first time, I would like a "Create profile" link to show in the nav-bar using the new_profile_path, but once that specific user has created his/her profile, i would like the "create Profile" to change to "View Profile" and remain that way permanently, which should redirect to the profile_path.
The code I currently have in my nav-bar is the following:
<ul>
<li><%= link_to root_path do %>
<i class="fa fa-home"></i>Main
<% end %>
</li>
<% if current_user.profile %>
<li><%= link_to profile_path(current_user.profile) do %>
<i class="fa fa-user"></i>Employee Profile<i class="fa fa-sort-desc"></i>
<% end %>
<% end %>
<% else %>
<li><%= link_to "New Profile", new_profile_path %></li>
<ul>
<li><%= link_to "Tasks / Activities", tasks_path %></li>
<li><%= link_to "Vacations", vacations_path %></li>
</ul>
</li>
<li><%= link_to projects_path do %>
<i class="fa fa-building-o"></i>Projects<i class="fa fa-sort-desc"></i>
<% end %>
<ul>
However even after creating the new profile the link "Create Profile" still remains. How can I fix this?
Try this:
<% if !current_user.profile.blank? %>
<%= link_to profile_path(current_user.profile) do %>
<i class="fa fa-user"></i>Employee Profile<i class="fa fa-sort-desc"></i>
<% end %>
<% else %>
<%= link_to "New Profile", new_profile_path %>
<% end %>
Your error is here <% end %><% else %>. You close the if test before your else. Result: <%= link_to "New Profile", new_profile_path %> is always displayed!
Remove the 10th line and it'll work fine.

Best way to display something based on if user logged in or not

I have an if statement that displays certain link based if a user voted or not for something. However, I'll get an error if the user is not logged in since current_user will be nil. I'm using devise for authentication.
undefined method `voted_as_when_voted_for' for nil:NilClass
<% if current_user.voted_as_when_voted_for #pin %>
<%= link_to dislike_pin_path(#pin), method: :put, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span> <%= #pin.get_upvotes.size %>
<% end %>
<% else %>
<%= link_to like_pin_path(#pin), method: :put, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span>
<%= #pin.get_upvotes.size %>
<% end %>
<% end %>
<%= link_to pins_path, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-step-backward"></span>
Back
<% end %>
<% if user_signed_in? %>
<%= link_to "Edit", edit_pin_path, class: "btn btn-default" %>
<%= link_to "Delete", pin_path, method: :delete, data: { confirm: "Are you sure"}, class: "btn btn-default" %>
<% end %>
What can I add to that if statement to display one of the links even if the user is not logged in without repeating link_to code. Using DRY Rails methodology. (if a user is not logged in and clicks like/dislike link, it will ask him to log in since I have my routes set up like this:
before_action :authenticate_user!, except: [:index, :show]
Try this:
<% if current_user && current_user.voted_as_when_voted_for #pin %>
... code here
<% else %>
<%= link_to like_pin_path(#pin), method: :put, class: "btn btn-default" do %>
<span class="glyphicon glyphicon-heart"></span>
<%= #pin.get_upvotes.size %>
<% end %>
<% end %>
In your case:
<% if signed_in? && current_user.voted_as_when_voted_for #pin %>
...
<% end %>
Assuming you're using devise since you included the authenticate_user! method.

Resources