Hiding a link from anonymous users - ruby-on-rails

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>

Related

Ruby on rails - user is nil after creating a session and redirecting home

I am building a rails app.
I am using devise to handle authentication
If the user is an admin, he or she should be able to see a link in the navbar that redirects to the whole list of profiles (for testing purposes)
<% if !user_signed_in? %>
<li class="nav-item">
<%= link_to "Sign up", new_user_registration_path, class: "nav-link" %>
</li>
<li class="nav-item">
<%= link_to "Log in", new_user_session_path, class: "nav-link" %>
</li>
<% else %>
<li class="nav-item">
<%= link_to "Log out", destroy_user_session_path, method: :delete, class: "nav-link" %>
</li>
<% if !#user.nil? && #user.admin? %>
<li class="nav-item">
<%= link_to "See all profiles", profiles_path, class: "nav-link" %>
</li>
<% end %>
<li class="nav-item">
<%= link_to "My profile", user_profile_path(current_user, current_user.user_profile), class: "nav-link" %>
</li>
<% end %>
However, said link only shows after going inside the my profile link. I did a raise in the UserProfiles controller to see what's going on and apparently the user is nil in the homepage even after logging in, hence the reason why the see all profiles link is not appearing
My guess is that I am missing an instance variable that provides the user id somewhere, (pages controller perhaps?) I don't have a users controller or user sessions controller yet. Basically, I don't understand why is the user nil after logging in and why is the log out link showing up without a user?
UserProfiles controller
class UserProfilesController < ApplicationController
before_action :set_user, only: [:index, :show]
def index
#user_profiles = UserProfile.all
end
def show
#user_profile = UserProfile.find(params[:id])
#user_profile.user = #user
end
private
def set_user
#user = current_user
end
end
Thanks for your help
You are not passing #user from the controller so cannot use in the view.
You should change your code to
<% if !#user_profile.user.nil? && #user_profile.user.admin? %>
<li class="nav-item">
<%= link_to "See all profiles", profiles_path, class: "nav-link" %>
</li>
<% end %>

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.

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.

Link to user profile

I'm trying to create a link that will direct people to the users profile page by clicking on their name when they look at their pics. The line of code is below however, I am not sure how to turn this into a link that will direct people to the users profile. Right now the users post displays their name in association with their post. Hopefully this makes sense Thanks in advance.
<p><strong><%= #post.user.name if #post.user %></strong></p>
The route to the users profile page is users_path.
Post/Show.html
<div class="row">
<div class="col-md-offset-4 col-med-8">
<div class="panel panel-default">
<div class="panel-heading center">
<% if #post.image.url %>
<%= image_tag #post.image.url(:medium) %>
<% elsif #post.video.url %>
<%= video_tag #post.video.url(:medium), controls: true, type: "video/mp4" %>
<% end %>
</div>
<div class="panel-body">
<p><%= #post.description %></p>
<p><strong><%= #post.user.name if #post.user %></strong></p>
<% if #post.user == current_user %>
<%= link_to edit_post_path(#post) do %>
<span class="glyphicon glyphicon-edit"></span>
Edit
<% end %>
<% end %>
<%= link_to 'Back', posts_path %>
</div>
</div>
Considering user_path is path to user's profile
<p>
<strong>
<%= link_to(user.name, user_path(user)) if user = #post.user %>
</strong>
</p>
I did this like this... <%= link_to (#post.user.name), user_path(#post.user) %>

Devise User Profile link in Navbar

I have the following code for the nav-bar in my application, using Devise for user authentication.
<ul class="nav pull-right">
<li class="">
<%= link_to root_path do %>
<i class="icon-home"></i> Home
<% end %>
</li>
<li class="">
<%= link_to current_user do %>
<i class="icon-book"></i> Portfolio
<% end %>
</li>
<li class="">
<%= link_to help_path do %>
<i class="icon-question-sign"></i> Help
<% end %>
</li>
<li class="">
<%= link_to(destroy_user_session_path, :method=>'delete') do %>
<i class="icon-signout"></i> Logout
<% end %>
</li>
</ul>
It works fine on the home page and the user profile page, but when I go to the other pages, such as the Projects or Help page( both of which have before_filter :authenticate_user! in their controllers ), it throws the following error
NoMethodError in Projects#index
undefined method `find_by_remember_token' for #<Class:0x3ec18d0>
It says that the offending code is this part:
<%= link_to current_user do %>
Which I use to link the user to his/her profile. Any ideas how to fix this error?
Update:
It seems like the problem solved itself after adding
#user = current_user
to the page's respective controllers.
Try to check if user signed in.
<%= link_to if_user_signed_in?, current_user %>

Resources