Extra blank <a></a> showing up - ruby-on-rails

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

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

Bootstrap Sass broke devise

Hi I hope someone can help. I i'm building a news application on rails. I installed devise and act-as-votable for posts. I then installed bootstrap-sass (twbs/bootstrap-sass) and devise wont render to the screen.
<% if user_signed_in? %>
<div class="index_log">
Hey, your logged in as <strong><%= current_user.email %></strong>.
<% if user_signed_in? %>
<%= button_to "Log Out", destroy_user_session_path, method: :delete, class: 'btn btn-default navbar-btn' %>
<% else %>
<%= link_to "Log In", new_user_session_path, class: 'btn btn-default navbar-btn', method: :get %>
<% end %>
When I go go to the login page, enter my username and password, nothing happens.
Or else I create a new user, again nothing happens.
Any Ideas - I would add code below but I'm not sure what to add.
Looks like you have an extra if user_signed_in? tag
Hey, your logged in as <strong><%= current_user.email %></strong>.
<% if user_signed_in? %>
and that is messing up your <% else %> (which now matches the extra if and is now nested within the first if user_signed_in? and will never be rendered.
Also, unrelated, but it should be "...you're logged in as..." and the div class="index_log" isn't closed

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>

What is `stringify_keys' in rails and how to solve it when this error comes

In a partial file of my application I have the following code snippet for displaying user navigation(through Devise):-
<ul class="nav pull-right">
<% if user_signed_in? %>
<li>
<%= current_user.email do %>
<i class=" icon-user icon-black"></i>
<% end %>
</li>
<li>
<%= link_to "Your Links", profiles_index_path do %>
<i class=" icon-user icon-black"></i>
<% end %>
</li>
<li>
<%= link_to "Sign out", destroy_user_session_path, :method => 'delete' do %>
<i class=" icon-user icon-black"></i>
<% end %>
</li>
<% else %>
<li>
<%= link_to "Login", new_user_session_path do %>
<i class=" icon-lock"></i>
<% end %>
</li>
<li>
<%= link_to "Sign up", new_user_registration_path do %>
<i class=" icon-home"></i>
<% end %>
</li>
<% end %>
</ul>
But I'm getting an error saying:-
undefined method `stringify_keys' for "/users/sign_in":String
Now my questions are:-
What is `stringify_keys' in general??
How do I resolve this in my code???
Thanks...
1) stringify_keys is a method that is called on a hash to convert its keys from symbols to strings. It's added by Rails - it's not a standard Ruby method. Here it is in the docs.
{:a => 1, :b => 2}.stringify_keys # => {"a" => 1, "b" => 2}
2) This means that your code is passing "/users/sign_in" somewhere that is expecting a hash. Closer inspection reveals that you are mixing and matching two forms of link_to:
# specify link contents as an argument
link_to "The text in the link", "/path/to/link", some: "options"
# specify link contents in a block
link_to "/path/to/link", some: "options" do
"The text in the link"
end
As you can see you are trying to do both:
<%= link_to "Sign out", destroy_user_session_path, :method => 'delete' do %>
<i class=" icon-user icon-black"></i>
<% end %>
and Rails expects the second argument in the block form to be the options hash, so it is calling stringify_keys on it which is causing your error.
Change those links to look like this instead:
<%= link_to destroy_user_session_path, :method => 'delete' do %>
<i class=" icon-user icon-black"></i> Sign out
<% end %>
as per the documentat stringify_keys will try to convert all keys to a string. So in simple works the method expects something with key values and convert its keys to strings.
In your case most probably your User object could be empty or a plan string. is this doesnot
work try posting the complete error log

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