Chaning image-button to text-button using Devise login - ruby-on-rails

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 %>

Related

Extra blank <a></a> showing up

When I go to my menu to sign in it works perfectly fine and there is nothing extra but when I am signed in there is an extra blank at the top. How can I fix this?
Server Side html.erb
<div id="myDropdown" class="dropdown-content">
<% if user_signed_in? %>
<a><%= link_to('My Account', edit_user_registration_path) %>
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
<% else %>
<%= link_to('Log in', new_user_session_path) %>
<%= link_to('Sign up', new_user_registration_path) %></a>
<% end %>
</div>
Client Side Html (Logged In)
<a></a>
My Account
<a rel="nofollow" data-method="delete" href="/users/sign_out">Sign out</a>
What it looks like
I think you don't have to write explicit tag
<%= link_to "Home", root_path %>
# => Home
So
<%= link_to('My Account', edit_user_registration_path %>
# => My Account
To remove the extra href tag one, just remove the tag and its closing from your code. and it will render the view correctly.
More Information about link_to helper method

How to Render Current User into Nav-Bar Button

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.

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.

user_signed_in? is always true

I have no user in my db
I have a rails3 application with devise. No change from default installation, but I have this strange problem:
<h1>Home</h1>
<% if (user_signed_in?) %>
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>.
<% end %>
Is always true. Also with no user and with no logged user. What could it be ????
Use current_user instead
<h1>Home</h1>
<% if current_user %>
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>.
<% end %>

Ruby on Rails Else If Question

I have an application where in the layout I have a user_name div which will display different things based on whether or not they are logged in, are an admin, etc. Right now my code is the following:
<% if current_user.role == "admin" %>
<p id="admintxt">You are an admin!</p>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% elsif current_user %>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% else %>
<%= link_to "Register", new_user_path %>
<%= link_to "Login", login_path %>
<% end %>
I have a current_user helper already and everything was working fine when the code was just:
<% if current_user %>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% else %>
<%= link_to "Register", new_user_path %>
<%= link_to "Login", login_path %>
<% end %>
Now when I make it an elsif statement, when I am logged in as an admin it works and I get the text displayed with the correct links. When I am not an admin user/logged out, I get a undefined method `role' for nil:NilClass error... Also my current_user stuff is declared in my application controller as follows:
helper_method :current_user
private
def current_user_session
return #current_user_session if defined?(#current_user_session)
#current_user_session = UserSession.find
end
def current_user
return #current_user if defined?(#current_user)
#current_user = current_user_session && current_user_session.record
end
Any ideas what I can do to display the result I want? "If they are a user with the role attribute equal to admin they get some text and the logged in links, if they are just a user they get the logged in links, and if they are not logged in they get the register and login links.
Thanks!
<% if current_user %>
<% if current_user.role == "admin" %>
<p id="admintxt">You are an admin!</p>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% else %>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% end %>
<% else %>
<%= link_to "Register", new_user_path %>
<%= link_to "Login", login_path %>
<% end %>
or with Rails >= 2.3
<% if current_user.try(:role) == "admin" %>
<p id="admintxt">You are an admin!</p>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% elsif current_user %>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% else %>
<%= link_to "Register", new_user_path %>
<%= link_to "Login", login_path %>
<% end %>
Hide the role check in the current user loop, which has the side effect of simplifying your conditional.
<% if current_user %>
<%= content_tag(:p, "You are an admin!", :id=>"admintxt") if current_user.role == "admin" %>
<%= link_to "Edit Profile", edit_user_path(:current) %>
<%= link_to "Logout", logout_path %>
<% else %>
<%= link_to "Register", new_user_path %>
<%= link_to "Login", login_path %>
<% end %>
<% if current_user and current_user.role == "admin" %>
This should prevent the error when there is no user logged in, but you could restructure the whole block in order to remove the redundant tests against current_user being nil.

Resources