Logout Error: No route matches [GET] "/users/sign_out" - ruby-on-rails

This error has been driving me and a fellow dev insane. We're building an app with Ruby/Rails jazz, and whenever I click to logout for a user session, I get this error:
Routing Error
No route matches [GET] "/users/sign_out"
Try running rake routes for more information on available routes.
Now I've ran rake routes a ton, and I get:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root / home#index
about /about(.:format) home#about
Is there anything to be done here? I've also followed the answers of a lot of the posts here on Stack Overflow to no avail. Anything else I can test to try to fix this problem?
EDIT: Here is logout link code
<a href="/users/sign_out" class="header-links right-link" data-method="delete" rel="nofollow">Logout</a

Inspect you HTML and make sure rails.js is loaded, and there are no javascript errors. And if you are using jQuery, make sure that there is noConflict.
Note: My guess is you are running rails version < 3.1, So check if these two lines are present in your layout
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>

Try:
<%= link_to "Logout", destroy_user_session_path, method: :delete %>

does your routes have this under "devise_for"? :
get 'users/sign_out' => 'sessions#destroy', :as => :destroy_user_session
And try this as link:
<%= link_to "Logout", destroy_user_session_path, method => :delete %>

Check to see if your routes.rb has,
devise_for :users, ActiveAdmin::Devise.config
Sometimes my installs seem to miss this part ActiveAdmin::Devise.configand then I get this error.
I see it in the gem's generator but sometimes it doesn't get added on my apps.

Related

undefined local variable or method with form_tag

I'm trying to create a simple search form in my Rails application. I get an error with the url path of the form:
<%= form_tag(med_search, :method => "get", id: "search-form") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %>
<%= button_to "Search", class: "btn btn-default" %>
<% end %>
The first line above causes error undefined local variable or methodmed_search' for #<#:0x007fab2b5afa90>`
The problem is most likely with my routes setup. I created a new controller action called search so I edited my routes.db to look like this:
resources :meds do
collection do
get 'search' => 'meds#search'
end
end
devise_for :users
#get 'meds/index'
root to: "meds#index"
resources :meds, :path => ''
end
When I do rake routes, I am seeing the path med search so I know the url is valid:
Prefix Verb URI Pattern Controller#Action
med_search GET /meds/:med_id/search(.:format) meds#search
meds GET /meds(.:format) meds#index
POST /meds(.:format) meds#create
new_med GET /meds/new(.:format) meds#new
edit_med GET /meds/:id/edit(.:format) meds#edit
med GET /meds/:id(.:format) meds#show
PATCH /meds/:id(.:format) meds#update
PUT /meds/:id(.:format) meds#update
DELETE /meds/:id(.:format) meds#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
root GET / meds#index
GET / meds#index
POST / meds#create
GET /new(.:format) meds#new
GET /:id/edit(.:format) meds#edit
GET /:id(.:format) meds#show
PATCH /:id(.:format) meds#update
PUT /:id(.:format) meds#update
DELETE /:id(.:format) meds#destroy
What should I change in the routes to fix this?
Thanks!
EDIT: changed url to med_search_path, get new error: No route matches {:action=>"search", :controller=>"meds"} missing required keys: [:med_id]. Looks like it's related to the route /meds/:med_id/search(.:format)
1) you need to change your form_tag like this
<%= form_tag(search_meds_path, :method => "get", id: "search-form") do %>
2) You need to change your route from member to collection like this
resources :meds do
collection do
get 'search' => 'meds#search'
end
end
3) Not sure why you need to add resources :meds, :path => '' at the bottom again. Incase you dont need it, it is better to remove.
It looks like you're trying to code up a search but because you put it in the resources block Rails is assuming you're talking about a specific med.
Remove the route from the resources block and change it to get 'meds/search' => 'meds#search'. That will allow you to use it as just a regular endpoint without Rails complaining that you need an ID.

Signing out with Devise not working

Im having problems with signing out with devise, i get the following error message when i try to sign out, its looking for a user with the id=sign_out. Any suggestions?
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with id=sign_out
Routes
devise_for :users
match 'users/settings', to: 'users#settings'
resources :users, only: [:show, :update]
Link
<%= link_to 'Logout', destroy_user_session_path, :method => :delete %>
Rake Routes
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
users_settings /users/settings(.:format) users#settings
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format)
Recently I've solved this problem, You need try to put or check the below line in your views/layouts/application.html.erb
<%= javascript_include_tag "application" %>
Also check if you have jquery_ujs in your Gemfile.
Make sure you're actually clicking on the correct link. I highly doubt that link routes the request to UsersController#show. destroy_user_session_path routes to /users/sign_out so this is impossible.

Can't get Devise path correct

I installed Devise to get authentication into my application. I followed all the instructions about the gem and that part seemed to go well.
However now that I am trying to link to the Register page, I get a crazy error I can't figure out... For some reason, rails can't figure out where the devise controllers are located...
from: public/index.html.erb (public landing page)
<div class ="getstarted">
<table>
<tr>
<td id = "introtext">
<p>Foo is good..l here is why: </p>
<ul>
<li>Improved quality</li>
<li>Technology consistencies </li>
<li>Cost efficiencies</li>
<li>Increased security</li>
<li>Improved adherence to compliance standards</li>
</ul>
</td>
<td id = "buttons">
<%= button_to "Sign Up for an Account", new_user_registration_path, :method => "get" %>
<br/>
<%= button_to "Log In to your Account", new_user_session_path, :controller => "devise/session", :method => "get" %>
</td>
</tr>
</table>
</div>
routes.rb has the following line:
devise_for :users
and the Routes table seems to have what it needs:
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
When I click on the button to register I go to the link:
http://localhost:3000/users/sign_up?
and Rails gives me the error:
No route matches {:controller=>"devise/public"}
Same thing for the log-in button:
http://localhost:3000/users/sign_in?
No route matches {:controller=>"devise/public"}
I set up a view for "public" to be my public viewable (pre authentication) pages... and I see that Rails is looking for that inside the devise directory (which doesn't exist).
I also tried deleting the ? at the end of the URL... that didn't produce any difference.
I'm stuck and a couple hours already invested. Hope someone can help me out.
For your "Log in to your account" button, I would change the current route from 'new_user_session_path,' to 'user_session_path'.
Also I would change 'button_to' into 'link_to'
See what that does

Devise routes issue in Rails 3.1 with No route matches [GET] "/users/sign_out"

I am using the Devise 1.4.9 authentication gem with my Rails 3.1 app and when I click on any link I get routing errors. Here's what happens when I click on the Sign Out link:
Routing Error
No route matches [GET] "/users/sign_out"
I am creating the link with this method:
<%= link_to('Sign Out', destroy_user_session_path, :method => :delete) %>
This is how the link is rendered in the source:
Sign Out
I have this line in my layout header:
<%= javascript_include_tag :defaults %>
These lines are in my application.js file:
//
// = require jquery
// = require jquery_ujs
// = require_tree .
Here is the relevant rake routes output:
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
I've tried pretty much everything in this thread to no avail:
No route matches "/users/sign_out" devise rails 3
I've restarted the server numerous times.
This issue seems to have come up in the past, but not a single solution described has changed this behavior and I'm stumped.
Please try to rename :defaults to :application when passing it to javascript_include_tag in case you define your includes in application.js
<%= javascript_include_tag :application %>
One, you might want to use a button (can be styled to look like a link) for something destructive, as per this checklist for using GET. That alone may fix it.
Two, you need the csrf_meta_tag in the file, which you probably already have.
Have you confirmed the JS files are actually being loaded?
(I thought the // = with the space might be a problem, but apparently it works as well as //= as Rails creates by default.)
Writing
devise_scope :user do
get "/users/sign_out", :to => "devise/sessions#destroy"
end
in my routes.rb solved the problem for me.

NoMethodError in Devise::Registrations#new on sign up with Devise and Refinery Cms 3.0

I am trying to Sign up on my app, I get this error
NoMethodError in Devise::Registrations#new
<h2>Sign up</h2>
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<div class="field">
undefined method `user_registration_path' for #<ActionDispatch::Routing::RoutesProxy:0x007fb0bc0dc220>
My rake routes output
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
reg GET /reg(.:format) reg#regl
refinery / Refinery::Core::Engine
// much refinery routes
I have already restarted Webrick and performed db:reset db:migrate
Also, when I am trying to access resource with console on web page I get
NameError: undefined local variable or method `resource' for #<ActionDispatch::Routing::RoutesProxy:0x007fb0c2b683a0>
here is my routes.rb
Rails.application.routes.draw do
devise_for :users
# This line mounts Refinery's routes at the root of your application.
# This means, any requests to the root URL of your application will go to Refinery::PagesController#home.
# If you would like to change where this extension is mounted, simply change the
# configuration option `mounted_path` to something different in config/initializers/refinery/core.rb
# We ask that you don't use the :as option here, as Refinery relies on it being the default of "refinery"
mount Refinery::Core::Engine, at: Refinery::Core.mounted_path
end
After 3 days s finaly slove it . add
Devise.setup do |config| config.router_name = :main_app end
in config/initializers/devise.rb

Resources