Adding a class to link_to - ruby-on-rails

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

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

Syntax error when passing method argument in link_to in rails

When I put :method => :delete in link_to I'm getting syntax error and I don't understand why.
Here's the code:
<%= link_to application_path(application), :method => :delete, :confirm => 'Are you sure?', :title => 'Delete task', :class => "color-red" do %>
<span class="glyphicon glyphicon-trash">
<% end %>

How to add a method to link_to do ViewHelper in Rails

Thats the original Devise logout link which works:
link_to("Logout", destroy_user_session_path, :method => :delete)
Because of the and the I had to switch to this kind of link. The problem now is the link try to find the user sign_out
Tried:
<%= link_to (destroy_user_session_path, :method => :delete) do %>
<i class="icon-off"></i> Logout</span>
<% end %>
and
<%= link_to (destroy_user_session_path, method => :delete) do %>
<i class="icon-off"></i> Logout</span>
<% end %>
and
<%= link_to (destroy_user_session_path, :method = 'delete') do %>
<i class="icon-off"></i> Logout</span>
<% end %>
Any ideas or suggestions?
I would do,
<span>
<i class="icon-off">
</i>
<%= link_to("Logout",destroy_user_session_path, :method => :delete)%>
</span>

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

Tidy up .html.erb output

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

Resources