I have this link
<li><a href="<%= destroy_user_session_path(:method => :delete) %>"><button
class="button icon-left ion-ios7-compose">Log out</button></a></li>
But it is not working. How do I fix the :method => :delete so that it works in the link?
I am getting this error:
No route matches [GET] "/users/sign_out"
Also, I am using devise for users.
use the link_to helper (also check out your html, a button isn't usually found inside a link).
<li><%= link_to 'Log out', destroy_user_session_path, method: :delete %></li>
You have to write it like this
<li><%= link_to "Log Out", destroy_user_session_path, :class => "button icon-left ion-ios7-compose", :method => :delete %></li>
Related
I am trying to log out a user from a (test) site in production on heroku.
The problem is heroku transforms this <%= link_to 'Logout', destroy_user_session_path, method: :delete %> in a GET /users/sign_out
I've tried this <%= link_to 'Logout', destroy_user_session_path, method: :delete, :data => { :no_turbolink => true } %> as suggested here Devise destroy_user_session_path does'nt work
I've found some solutions in reply to destroy_user_session_path is triggering GET instead of DELETE in Rails but I don't feel comfortable with the answers as I'd like to avoid a GET.
Logout was ok in development, so I checked on https://devcenter.heroku.com/ and made some modifications, but I am still stuck.
I worked on the asset pipeline and added these gems:
gem 'rails_serve_static_assets'
gem 'rails_stdout_logging'
These gems are probably useful in other cases, but not mine (I also have a problem with links to images that don't persist).
EDIT
Here is my full template
<div class="" style="background-color: #1a252f;">
<ul class="nav justify-content-center">
<% if user_signed_in? %>
<li class="nav-item">
<%= link_to 'Edit account|', edit_user_registration_path %></li>
<li class="nav-item">
<%= link_to 'Logout', destroy_user_session_path, method: :delete, :data => { :no_turbolink => true } %></li>
<% else %>
<li class="nav-item">
<%= link_to 'Login', new_user_session_path %></li>
<% end %>
</ul>
</div>
EDIT 2
I've also tried this in routes.rb:
devise_for :users do
get "/users/sign_out" => "devise/sessions#destroy", :as => :destroy_user_session
end
and I still get a status 404 in the heroku logs.
The solution provided here: destroy_user_session_path is triggering GET instead of DELETE in Rails was the one for me,
i.e. with config.sign_out_via = :get in devise.rb
I am running Rails 5 and updated Bootstrap to version 4 in an existing app which uses Devise.
Before updating Bootstrap, the routes were working fine. Now, when the user selects sign-out, they receive the following message:
No route matches [GET] "/users/sign_out"
Here is part of the header code:
<% if current_user %>
<a class=<%= link_to 'Classes', tracks_path %></a>
<a class=<%= link_to 'Edit profile', edit_user_registration_path %></a>
<a class=<%= link_to 'Sign out', destroy_user_session_path, method: :delete %></a>
And Rake Routes shows:
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
Is there a reason that the change to Bootstrap 4 would trigger this error? I have followed all of the instructions on the Bootstrap repo for installation.
The problem is you're rendering your link tags inside the class attribute:
<a class=<%= link_to 'Sign out', destroy_user_session_path, method: :delete %></a>
So that results in malformed output which ignores that you specify method: :delete. This is what you're looking for:
<%= link_to "Sign out", destroy_user_session_path,
method: :delete, class: 'nav-link' %>
I have been googling last 2 hours and unfortunately I cannot fix this, though I thought it must be easy.
<%= link_to 'Log Out', {:controller => 'static_pages', :action => 'index'} do %>
<i class="glyphicon glyphicon-log-out link-icon"></i>
<% end %>
This the code I have. So, I need "Log Out" link to appear with log-out icon, which is from bootstrap. But HTML output of this code is this:
<a controller="static_pages" action="index" href="Log Out">
<i class="glyphicon glyphicon-log-out link-icon"></i>
</a>
I tried many ways to fix this, but I also tried removing "do" so, I can see if it causes the problem. This is the link after I removed "do":
<%= link_to 'Log Out', {:controller => 'static_pages', :action => 'index'} %>
HTML output of this is:
Log Out
It doesn't make sense. Because I have same link_to in my footer and it works. Footer:
<%= link_to 'Log In', {:controller => 'users', :action => 'login'} %>
Footer output:
Log In
So, why this isn't working in another page? Thank you.
When you pass a block to the link_to helper this will be a body for it.
<%= link_to :controller => 'static_pages', :action => 'index' do %>
<i class="glyphicon glyphicon-log-out link-icon"></i>
Log Out
<% end %>
How can I go about embedding font-awesome icons into my Ruby <%= link_to code?
The below does NOT work, is it possible to accomplish the below some how so that it actually works?
<li><%= link_to "<span class="fa fa-minus-circle fa-1x"></span> Settings</span>", destroy_user_session_path, :method => :delete %></li>
Thanks!
Johnson
have you try this:
<li><%= link_to your_path do %><span class="fa fa-minus-circle fa-1x"></span> Settings <% end %></li>
for instance with font-awesome in some of my codes:
<li><%= link_to edit_contact_path(#contact) do %><i class="fa fa-pencil-square-o"></i> Edit<% end %></li>
<li><%= link_to #contact, method: :delete, data:{confirm: "Delete this contact?"} do %><i class="fa fa-exclamation-triangle"></i> Delete<% end %></li>
Here is the documentation, see the section with link_to ... do .... end
link to documentation
I've always done it including the class in the Ruby code like this,
<li><%= link_to " Sign Up", '#', class: "fa fa-user-plus" %></li>
This includes the icon on the left of the text so you have to include the space preceding "Sign up" in order to get it to look right.
Better option for you is use this gem:
https://github.com/h4b00/awesome_link
After installation you can simply use(for example):
<%= awesome_link('fa-pencil-square-o', root_path, method: :update) %>
I'm using devise for user authentication. In my views I've set:
<% if user_signed_in? %>
<li><%= link_to "Log Out", destroy_user_session_path %></li>
<% else %>
<li><%= link_to "Sign In", new_user_session_path %></li>
<% end %>
However when I click Log_Out I'm getting an error:
No route matches [GET] "/users/sign_out"
However when I check my rake routes I'm getting:
devise/sessions#destroy destroy_user_session DELETE /users/sign_out(.:format)
What apneadiving said.
<%= link_to "Log Out", destroy_user_session_path, method: :delete %>
Default sign out is use "delete" method. Your route also said the method is “DELETE"
If you want use "get" method
Modify devise.rb to
config.sign_out_via = :get
Baloo is right, make sure to use the :delete method. You can see this clearly if you invoke
rake routes
You'll see the path as well as the method.