Ruby on Rails Else If Question - ruby-on-rails

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.

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

Allow unauthenticated users to access views that utilize current_user

I have lots of views that have elements that check if the current user isn't the same user that's being viewed, etc. For example, on the user show page there is a button that allows a user to follow another user, which doesn't appear if the user is looking at their own profile.
<% if current_user != #user %>
<div id="follow_form">
<% if current_user.following?(#user) %>
<%= render 'users/unfollow' %>
<% else %>
<%= render 'users/follow' %>
<% end %>
</div>
<% end %>
The problem arises that if you're not logged in, rails throws an error.
undefined method `id' for nil:NilClass
<% if current_user.id == #user.id %>
<%= link_to "Edit Profile", edit_user_registration_path(#user) %>
<% else %>
<%= link_to 'message', new_message_path(receiver_id: #user.id) %>
...
I don't want to have to force people to log in or sign up to view index or show pages. How can I get around this?
You should try that:
<% if user_signed_in? %>
<% if current_user != #user %>
<div id="follow_form">
<% if current_user.following?(#user) %>
<%= render 'users/unfollow' %>
<% else %>
<%= render 'users/follow' %>
<% end %>
</div>
<% end %>
<% end %>

Linking to basket/cart in ruby on rails

I have a menu in my header that has a show basket and a login button, each work when the code is placed in separately but not when both lines are in the file.
I'm using devise for the users.
Is there a better way to link to the current basket?
<li><%= link_to basket_path(#basket.id) do %>
<%= image_tag "/assets/viewBasket.png" %>
</li>
<% end %>
<% if signed_in? %>
<li><%= link_to edit_user_registration_path do%>
<%= image_tag"/assets/my_account.png" %></a></li>
<% end %>
<li><%= link_to destroy_user_session_path do%>
<%= image_tag"/assets/logout.png" %></li>
<%end%>
<% else %>
<li><%= link_to new_user_session_path do%>
<%= image_tag"/assets/loginRegisterBtn.png" %></li>
<% end%>
<% end %>
If I run on its own this works but not with the code after.
<li><%= link_to basket_path(#basket.id) do %>
<%= image_tag "/assets/viewBasket.png" %></li>
<% end %>
I think its to do with the way the current basket is set with the session id in the current_basket model.
module CurrentBasket
private
def set_basket
#basket = Basket.find(session[:basket_id])
rescue ActiveRecord::RecordNotFound
#basket = Basket.create
session[:basket_id] = #basket.id
end
end
The closing of <li> must be after the end of the link, like this:
<li>
<%= link_to basket_path(#basket.id) do %>
<%= image_tag "/assets/viewBasket.png" %>
<% end %>
</li>
I used the answer above which helped with one issue, however, I found that I had defined only the shop and index page. Removing this and it now works.
include CurrentBasket
before_action :set_basket, only: [:index, :shop]

One of my rails navbar links doesn't show up, but the rest do?

I can not get my new blogs link to show up in my navigation bar. I am doing the same thing as I always do and my links always work. I have tried doing new_blogs_path new_blog_path and specifying the controller and action.
Here is my navigation links partial:
<%= link_to "About", controller: "static", action: "about", id: #about %>
<%= link_to "Testimonials", controller: "static", action: "testimonials", id:
#testimonials %>
<%= link_to "Store", controller: "products", action: "index" %>
<%= link_to "New Product", new_product_path %>
<% if current_user %>
<%link_to "New Blog", new_blog_path %>
<%= link_to "Log Out", logout_path %>
<%= current_user.email %>.
<% else %>
<%= link_to "Sign Up", signup_path %>
<%= link_to "Log In", login_path %>
<% end %>
The new blog code can be found in the if/else statement. Thanks for the help.
Answer my other question here too: Keep getting "no file selected" when attempting to upload an image using carrier wave?
You'll need to use <%= link_to "New Blog", new_blog_path %>
Use <%= when you want it to be visible to the user. Otherwise, use <% without an equals sign.

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

Resources