How to add a method to link_to do ViewHelper in Rails - ruby-on-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>

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

Extra blank <a></a> showing up

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

How do I generate a <button> tag with an <i> inside?

I want to produce this HTML:
<a class="action-btn btn-delete" href="/product/9">
<i class="icon-trash icon-white"></i>
</a>
But I read that I need to use the button_to helper method, rather than link_to.
So I tried this:
<%= button_to product, method: :delete, class: "action-btn btn-delete" do %>
<i class="icon-edit icon-white"></i>
<% end %>
But that doesn't work.
Thoughts?
I think you can use the link_to:
<%= link_to product, :method => "DELETE", :class => "action-btn btn-delete" do %>
<i class="icon-edit icon-white"></i>
<% end %>
I don't see how you can't use [link_to][1]
<%= link_to product, class: "action-btn btn-delete" do %>
<i class="icon-edit icon-white"></i>
<% end %>
Try, it may work,
<%= link_to product, method: :delete, class: "action-btn btn-delete" do %>
<i class="icon-edit icon-white"></i> Delete
<% end %>

How do I do an <i> tag within a link_to helper?

I would like to replicate this:
<i class="icon-pencil"></i>Take The Pledge
I tried this:
<%= link_to("Take the pledge", root_path, :class => "btn btn-inverse btn-mini btn-ten") do %>
<i class="icon-pencil"></i>
<% end %>
But it gave me this error:
NoMethodError at /
undefined method `stringify_keys' for "/":String
At the link_to call.
Thoughts?
According to the documentation you need to put the (complete) link text in the block, like this:
<%= link_to(root_path, :class => "btn btn-inverse btn-mini btn-ten") do %>
<i class="icon-pencil">Take the pledge</i>
<% end %>
<%= link_to(root_path, :class => "btn btn-inverse btn-mini btn-ten") do %>
<i class="icon-pencil"></i>
Take The Pledge
<% end %>

Embed additional HTML inside of link_to call

How do I embed additional HTML inside of a link_to call?
Here is my desired result:
<i class="icon-show"></i>Show
Here is my link_to call
<%= link_to "Show", exercise_path(exercise) %>
<%= link_to '<i class="icon-search"></i> Show'.html_safe, exercise_path(exercise), :class => 'btn btn-small' %>
.html_safe is required so that it is not escaped.
It is cleaner if you wrap it in a do block
<%= link_to exercise_path(exercise) do %>
<i class="icon-search"></i> Show
<% end %>

Resources