Tidy up .html.erb output - ruby-on-rails

My application layout has a login/logout link that displays depending on whether you are signed in or not:
<% if signed_in? %>
<%= link_to "Sign out", signout_path, :method => :delete %>
<% else %>
<%= link_to "Sign in", signin_path %>
<% end %>
This works fine, but seems really untidy and verbose. How can I output the appropriate link_to without so many <%...%> brackets?

I'd go with HAML, but if you don't want to use HAML, you could use a ternary operator:
<%= signed_in? ? link_to( 'Sign Out', signout_path, :method => :delete ) : link_to( 'Sign In', signin_path ) %>

Use HAML ;)
- if signed_in?
= link_to "Sign out", signout_path, :method => :delete
- else
= link_to "Sign in", signin_path

You could use the concat helper: (ActionView::Helpers::TextHelper)
<%
if signed_in?
concat link_to( "Sign out", signout_path, :method => :delete )
else
concat link_to( "Sign in", signin_path )
end
%>
Though in the rails api they prefer the <% %> syntax....

Try adding a - (dash) to your closing tag, like so:
<% if signed_in? -%>
<%= link_to "Sign out", signout_path, :method => :delete -%>
<% else -%>
<%= link_to "Sign in", signin_path -%>
<% end -%>
From: http://www.plexusweb.com/staff/travis/blog/post/117/Rails-inline-ERb-effects-on-HTML-structure
Edit;
My bad, thought is was about formatting the HTML output (newlines etc.)

With 2 open-close brackets you could do it like this:
<%= link_to( "Sign out", signout_path, :method => :delete ) if signed_in? %>
<%= link_to( "Sign in", signin_path) if !signed_in? %>

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

Adding a class to link_to

I have been trying to add a class to my link_to for over an hour now. No matter what I try, I get an error.
<% if user_signed_in? %>
<li>
<%= link_to('Sign out', destroy_user_session_path, :method => :delete) %>
</li>
<% else %>
<li>
<%= link_to('Sign in', new_user_session_path) %>
</li>
<% end %>
I am trying to add :class => "page-scroll btn-signin" to both of my link_to lines.
I solved the problem
<%= link_to 'Sign out', destroy_user_session_path, :method => :delete, :class => "page-scroll btn-signin" %>
This code works correctly.
<%= link_to 'Sign in', new_user_session_path, :class => "page-scroll btn-signin" %>
and
<%= link_to 'Sign out', destroy_user_session_path, :method => :delete, :class => "page-scroll btn-signin" %>
More info
EDIT
Destructive actions should be performed as a form submission - link
use button_to on Sign Out.
<%= button_to "Sign out", destroy_user_session_path, :method=>:delete, :class => "page-scroll btn-signin" %>

How can I get the 'Sign Up' link in the header to disappear after the user logs in

enter image description here
I am developing my first RAILS application whilst using the Devise GEM for authentication.
I would like users who have Signed Up already and are subsequently Signed In to my application (to have the "Signed Up' link in the header not be made available to them).
Can someone kindly guide me as to how I can modify my attached syntax to provide this.
Thank you in advance of your help.
enter image description here
Using the Devise gem for user authentication, you can verify if a user is signed in using the user_signed_in? helper method:
<% if user_signed_in? %>
<%= button_to "SIGN OUT", destroy_user_session_path, :method => :delete %>
<% else %>
<%= button_to "SIGN IN", new_user_session_path, :method => "get" %>
<%end %>
Here you are checking if the user_signed_in? return true and if so, you will show the sign out path to destroy a user's session.
From the link you provided (http://i.stack.imgur.com/ZZU3P.png) your problem is that you did not wrap the <%= link_to "signup", new_user_session_path %> into the conditional statement.
So to solve your problem you should do this:
<% if user_signed_in? %>
<%= link_to "logout", destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to "signup", new_user_registration_path %>
<%= link_to "signin", new_user_session_path %>
<%end%>

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