No route matches [GET] "/users/sign_out" - authentication

Here is my actual error: No route matches [GET] "/members/sign_out"
Since most people will use "users" I thought it would be more helpful to have that in the title. At any rate, I am essential unable to logout. I can successfully edit my member profile.
I am using devise 1.4.2 and Rails 3.1.0.rc4. Also, I have generated two separate devise models - one called "members" and the other called "admins". I was able to register and log into both of them (simultaneously) by manually navigating to the correct URL path (i.e., localhost:3000/admins/sign_in/). I created some links within my application.html.haml layout file by following this RailsCast on Devise. I am aware that it only addresses signin/signout links for "members."
If I click on the signout link I get the above error. This occurs if I manually navigate to either signout URL (i.e., localhost:3000/admins/sign_out/).
Can someone tell me why this is happening? Below are the various related files. And of course, I'm a newbie...
rake routes output:
j(film_repo)$ rake routes
new_member_session GET /members/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
member_session POST /members/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_member_session DELETE /members/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
member_password POST /members/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_member_password GET /members/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_member_password GET /members/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /members/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_member_registration GET /members/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
member_registration POST /members(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_member_registration GET /members/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_member_registration GET /members/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /members(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /members(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
new_admin_session GET /admins/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
admin_session POST /admins/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_admin_session DELETE /admins/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
admin_password POST /admins/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_admin_password GET /admins/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_admin_password GET /admins/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /admins/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_admin_registration GET /admins/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
admin_registration POST /admins(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_admin_registration GET /admins/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_admin_registration GET /admins/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /admins(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /admins(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
films GET /films(.:format) {:action=>"index", :controller=>"films"}
POST /films(.:format) {:action=>"create", :controller=>"films"}
new_film GET /films/new(.:format) {:action=>"new", :controller=>"films"}
edit_film GET /films/:id/edit(.:format) {:action=>"edit", :controller=>"films"}
film GET /films/:id(.:format) {:action=>"show", :controller=>"films"}
PUT /films/:id(.:format) {:action=>"update", :controller=>"films"}
DELETE /films/:id(.:format) {:action=>"destroy", :controller=>"films"}
root / {:controller=>"films", :action=>"index"}
routes.rb
FilmRepo::Application.routes.draw do
devise_for :members
devise_for :admins
resources :films
root :to => 'films#index'
end
admin.rb (model)
class Admin < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, and :omniauthable
devise :database_authenticatable, :registerable, :timeoutable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
member.rb (model)
class Member < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
end
application.html.haml
!!!
%html
%head
%title Film Repo
= stylesheet_link_tag 'compiled/screen.css', :media => 'screen, projection'
= stylesheet_link_tag 'compiled/print.css', :media => 'print'
/[if lt IE 8]
= stylesheet_link_tag 'compiled/ie.css', :media => 'screen, projection'
= csrf_meta_tag
%body.bp
#container
#user_nav
- if member_signed_in?
Signed in as #{current_member.email}. Not you?
\#{link_to "Sign out", destroy_member_session_path}
- else
= link_to "Sign up", new_member_registration_path
or #{link_to "sign in", new_member_session_path}
- flash.each do |name, msg|
= content_tag :div, msg, :id => "flash_#{name}"
= yield

You can end a session via get by changing the devise configuration in initializers.
# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = :get
Just open the link and your session is removed.

I had a similar problem, but addition of the :method=> :delete didn't work.
I was able to add a new route for a the get request by commenting out the devise_for :users and adding
devise_for :users do
get '/users/sign_out' => 'devise/sessions#destroy'
end

I had a similar problem.
My view code was like this:
<%= link_to " exit", destroy_user_session_path, method: :delete %>
After adding the following change to routes.rb it worked,
devise_for :users
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end

Although I don't know the cause, the reason why you are getting that message is because in your routes you have
destroy_member_session DELETE /members/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
Which means that route is only available with the DELETE method as opposed to GET. This is a bit weird since in the docs for devise it says that it should create it as GET route (https://github.com/plataformatec/devise/blob/master/lib/devise/rails/routes.rb#L30)
With it as a DELETE route, you should be able to logout using
link_to :logout, destroy_member_session_path, :method => :delete

I just needed to add the
//= require jquery
//= require jquery_ujs
to my application.js

If you want to use :delete method for security reasons and not be dependent on jquery-ujs you can use button_to instead of link_to, like:
button_to "Log out", destroy_user_session_path, method: :delete
if using link_to you must be sure to have javascript active:
Note that if the user has JavaScript disabled, the request will fall
back to using GET.
As seen in docs

In devise.rb, change
config.sign_out_via = :delete
to
config.sign_out_via = :get
This worked for me. I went crazy with this because the default is delete and I don’t understand why.
This works, but I am not sure whether it affects other elements in the application.

We still can use :method => :delete in my code, like that
= link_to "Sign out", destroy_user_session_path,:method => :delete
The reason i think we fail to load javascript that include jquery, make sure
= javascript_include_tag "application" (haml- you can use html too)
to include jquery-ui and jquery-ujs. So if it still error, i suggest to change rails gem in GEMFILE to version 3.2.6 and call bundle update to update gems. It works for me!

= link_to "Sign out", destroy_user_session_path,:method => :delete
will NOT work instead use this,
= link_to "Sign out", destroy_user_session_path,:method => 'delete'
should do the trick or worse case add require jquery_ujs in your application.js

In Rails 6:
I just changed the link_to to button_to, and the 'sign out' works properly
<%= button_to "Sign out", destroy_user_session_path, method: :delete %>

In rails 7, you need to add data: { turbo_method: :delete" } to link_to. So the link_to would look like this
<%= link_to "Log out", destroy_user_session_path, data: { turbo_method: :delete } %>

Using Rails4, I had to use the following method:
<%= link_to "Logout", destroy_admin_session_path, method: :delete %>
Emphasis on where the colons are on method: and :delete

Maybe that will help somebody.
Upgraded from Rails 3.0 to 3.1 and found this problem.
This fixed it for me:
routes.rb:
devise_for: users
devise.rb:
config.sign_out_via = :delete
application.html.erb:
<%= javascript_include_tag "application" %>
* not :defaults
_login_items.html.erb:
<%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
app/assets/javascripts/application.js
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require_tree .
and I had in javascript/ jquery.js, jquery_ujs.js from 3.0 version that I've removed.

You may have removed assets/javascripts/*
Run rails generate jquery:install --ui this will generate all the javascripts as shown below
xxxx#xxxxx:~/Projects/Rails_apps/rtest$ rails generate jquery:install --ui
remove public/javascripts/prototype.js
remove public/javascripts/effects.js
remove public/javascripts/dragdrop.js
remove public/javascripts/controls.js
copying jQuery (1.7.1)
create public/javascripts/jquery.js
create public/javascripts/jquery.min.js
copying jQuery UI (1.8.16)
create public/javascripts/jquery-ui.js
create public/javascripts/jquery-ui.min.js
copying jQuery UJS adapter (822920)
remove public/javascripts/rails.js
create public/javascripts/jquery_ujs.js
Go to your layout e.g application.html.erb and edit <%= javascript_include_tag :all %>
That worked for me :)

FWIW I have also run into this problem. Have looked into all of the suggested answers however the only one which worked was to foto open routes.rb and comment out the following line:
devise_for :users
Below that, add the following line:
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end

The problem begins with rails 3.1 in assets/javascript/. Just look for application.js, and if the file doesn't exist, create a file with that name. I don't know why my file disappears or never was created on rails new app... that file is the instance for jquery.

#creamhost say,
devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' end
but it is not correct solution for me (Rails4). I solved our problem (#Olives' answer),
link_to :logout, destroy_member_session_path, method: :delete

I am using rails version 5. I encounter this problem also. The simple fix I did was to changing the devise configuration in initializes to the default.
From :
config.sign_out_via = :get
To :
config.sign_out_via = :delete

Keep your devise.rb using the correct HTTP method:
# good
config.sign_out_via = :delete
# bad
config.sign_out_via = :get
Use button_to instead of link_to
# good
= button_to "Sign Out", destroy_user_session_path, method: :delete
# bad
= link_to "Sign Out", destroy_user_session_path, method: :delete"
If you are using bootstrap (keep it classy)
= link_to "Sign Out", destroy_user_session_path, method: :delete, class: "btn btn-default btn-sm"
Ref: github.com/heartcombo/devise/issues/4570#issuecomment-740812109

Just use the following for your sign out link:
<%= link_to "Sign out", destroy_user_session_path, method: :delete %>

//= require jquery_ujs
You are missing this line in your assets. There's no need to get /users/signout request. Put this line into JavaScript file at very top of the page.

Had the same problem and remembered it only started happening after I decided to "clean up" my Javascript files. So I ran rails generate jquery:install --ui again and this solved it for me. (You can ignore the --ui part if you don't need JQuery UI, I suppose.)

devise_for :users
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end
That works for me

I believe this error is due to devise library template not aligned with changes on rails 7,
In short, you are not sending the request in method delete as expected due to the Javascript library that does that is now missing.
you can change :
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
with
<%= link_to "Sign out", destroy_user_session_path, data: { "turbo-method": :delete }, class: "btn btn-danger ml-3" %>
And it will work again

This worked for me:
<%= link_to 'Sign out', destroy_user_session_path, data: { turbo_method: :delete } %>

It happens only on windows.. Add the following thing to your Application.html.erb file.
devise_for :users
devise_scope :user do
get '/users/sign_out' => 'devise/sessions#destroy'
end

Related

Devise, can't log out

In a Rails app, I use devise to manage my users and my link to destroy a session no longer work. It was working, and now I have add active admin, It doesn't.
My link is
<%= link_to "Déconnexion", destroy_user_session_path, :method => :delete, :class => 'button' %>
My routes.rb
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks"}
My rake routes
destroy_user_session DELETE /users/sign_out(.:format)
And it try to open the view /users/sign_out, so I have :
ActiveRecord::RecordNotFound in UsersController#show
Couldn't find User with id=sign_out
Does Active_admin and Devise crash together?
It will be weird 'cause active use devise, no?
Edit:
For the next person who will have this issue, I solved it by adding the next line to /config/initializers/devise.rb.
config.sign_out_via = :get
Not exactly the finest way, but it does the job.
Posting Jeff Paquette's comment as an answer.
Update the config/initializers/active_admin.rb with:
config.logout_link_method = :delete
Please make changes in your routes.rb :-
devise_scope :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks"} do
get "/users/sign_out", :to => "devise/sessions#destroy"
end
I am also getting same problem, only this can resolve me after 1hr time wasting.
Thanks.

Routing error (GET instead of POST) with Vote_fu/Thumbs_up gem

I am trying to use the vote_fu gem and I encounter a problem that other have as well, but I can't solve this with others' solutions..
I don't think the problem is related directly to the gem.. but rather to some wrong route or a missing parameter..
I have a Msg model which I included the act_as_voteable.
For the User Model I added act_as_voter
In the Msg controller I added
def votefor
#msg= Msg.find(params[:id])
current_user.vote_for(#msg)
redirect_to :back
end
In routes:
resources :msgs do
member do
post :votefor
end
end
And to the show of Msg I added
<%= link_to "Vote Up", votefor_msg_path(#msg), :method => :post %>
But when I click on the link created I get
Routing Error
No route matches [GET] "/msgs/1/votefor"
Why does it 'GET' instead of 'POST'? What am I missing?
I think its very late to answer this question & I mostly think you must have figured it out... I ran into the same issue & I understood the reason why..
This is a sample comment from routes.rb
# Sample resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
the post 'toggle' here is not the post-controller but :method => 'post'
link_to default uses :method => 'get' but you can override it using :method => 'post'
if u use link_to .. in the example without :method => 'post'
your routing should be
resources :msgs do
member do
get :votefor
end
end
Hope this helps!
Update:
For the doubt you have:
link_to with a :method => :post
use this
<%= link_to "Vote Up", votefor_msg_path(#msg), :method => :post %>
and in the routes it should be
resources :msgs do
member do
post :votefor
end
end
the post :votefor .. is :method => :post not the posts controller.
Do check for brady8's answer

Rails routing using destroy action

In my app, comments belong to photos and I am trying to add a method to destroy a comment.
routes.rb
resources :photos do
resources :comments
end
CommentsController
def destroy
#comment = Comment.find(params[:id])
#comment.destroy
end
photos/show.html.erb
<% #photo.comments.each do |comment| %>
<%= comment.body %></p>
<p><%= link_to 'Remove comment', comment, :confirm => 'Are you sure you want to remove this comment? This cannot be undone.', :method => :delete %></p>
<% end %>
The error I get is undefined method 'comment' for #<Photo:0x10ace9270>.
I think I may not have my routes setup correctly because when I check the routes for comment I get:
rake routes | grep comment
photo_comments GET /photos/:photo_id/comments(.:format) {:action=>"index", :controller=>"comments"}
POST /photos/:photo_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_photo_comment GET /photos/:photo_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_photo_comment GET /photos/:photo_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
photo_comment GET /photos/:photo_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /photos/:photo_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /photos/:photo_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
Anyone have thoughts as to where I went wrong here? Thanks.
<% #photo.comments.each do |comment| %>
<%= comment.body %></p>
<p><%= link_to 'Remove comment', [#photo, comment], :confirm => 'Are you sure you want to remove this comment? This cannot be undone.', :method => :delete %></p>
<% end %>

How do I remove the Devise route to sign up?

I'm using Devise in a Rails 3 app, but in this case, a user must be created by an existing user, who determines what permissions he/she will have.
Because of this, I want:
To remove the route for users to sign up.
To still allow users to edit their profiles (change email address and password) after they have signed up
How can I do this?
Currently, I'm effectively removing this route by placing the following before devise_for :users:
match 'users/sign_up' => redirect('/404.html')
That works, but I imagine there's a better way, right?
Update
As Benoit Garret said, the best solution in my case is to skip creating the registrations routes en masse and just create the ones I actually want.
To do that, I first ran rake routes, then used the output to re-create the ones I wanted. The end result was this:
devise_for :users, :skip => [:registrations]
as :user do
get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
put 'users' => 'devise/registrations#update', :as => 'user_registration'
end
Note that:
I still have :registerable in my User model
devise/registrations handles updating email and password
Updating other user attributes - permissions, etc - is handled by a different controller
Actual answer:
Remove the route for the default Devise paths; i.e.:
devise_for :users, path_names: {
sign_up: ''
}
you can do this in your model
# typical devise setup in User.rb
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
change it to:
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
notice that the symbol :registerable was removed
That's it, nothing else is required. All routes and links to registration page are magically removed too.
I tried to do this as well, but a thread on the devise google group dissuaded me from searching for a really clean solution.
I'll quote José Valim (the Devise maintainer) :
There isn't a straight-forward option. You can either provide a patch
or use :skip => :registerable and add only the routes you want.
The original question was :
Is there any good way to remove a specific route (the delete route)
from Rails?
I had similar issue tried to remove devise_invitable paths for create and new :
before:
devise_for :users
rake routes
accept_user_invitation GET /users/invitation/accept(.:format) devise/invitations#edit
user_invitation POST /users/invitation(.:format) devise/invitations#create
new_user_invitation GET /users/invitation/new(.:format) devise/invitations#new
PUT /users/invitation(.:format) devise/invitations#update
after
devise_for :users , :skip => 'invitation'
devise_scope :user do
get "/users/invitation/accept", :to => "devise/invitations#edit", :as => 'accept_user_invitation'
put "/users/invitation", :to => "devise/invitations#update", :as => nil
end
rake routes
accept_user_invitation GET /users/invitation/accept(.:format) devise/invitations#edit
PUT /users/invitation(.:format) devise/invitations#update
note 1 devise scope https://github.com/plataformatec/devise#configuring-routes
note 2 I'm applying it on devise_invitable but it will work with any devise *able feature
Important note: see that devise_scope is on user not users ? that's correct, watch out for this ! It can cause lot of pain giving you this problem:
Started GET "/users/invitation/accept?invitation_token=xxxxxxx" for 127.0.0.1
Processing by Devise::InvitationsController#edit as HTML
Parameters: {"invitation_token"=>"6Fy5CgFHtjWfjsCyr3hG"}
[Devise] Could not find devise mapping for path "/users/invitation/accept? invitation_token=6Fy5CgFHtjWfjsCyr3hG".
This may happen for two reasons:
1) You forgot to wrap your route inside the scope block. For example:
devise_scope :user do
match "/some/route" => "some_devise_controller"
end
2) You are testing a Devise controller bypassing the router.
If so, you can explicitly tell Devise which mapping to use:
#request.env["devise.mapping"] = Devise.mappings[:user]
I found another post similar to this one and wanted to share an answer #chrisnicola gave. In the post they were attempting to only block user signup's during production.
You could also modify the registrations controller. You can use something like this:
In "app/controllers/registrations_controller.rb"
class RegistrationsController < Devise::RegistrationsController
def new
flash[:info] = 'Registrations are not open.'
redirect_to root_path
end
def create
flash[:info] = 'Registrations are not open.'
redirect_to root_path
end
end
This will override devise's controller and use the above methods instead. They added flash messages incase that someone somehow made it to the sign_up page. You should also be able to change the redirect to any path you like.
Also in "config/routes.rb" you can add this:
devise_for :users, :controllers => { :registrations => "registrations" }
Leaving it like this will allow you to use the standard devise edit your profile. If you wish you can still override the edit profile option by including
def update
end
in the "app/controllers/registrations_controller.rb"
This is an old question - but I recently had solve the same issue and came up with a solution which is far more elegant than:
devise_for :users, :skip => [:registrations]
as :user do
get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
put 'users' => 'devise/registrations#update', :as => 'user_registration'
end
And it gives the default names for the named routes (like cancel_user_registration) without being excessively verbose.
devise_for :users, skip: [:registrations]
# Recreates the Devise registrations routes
# They act on a singular user (the signed in user)
# Add the actions you want in 'only:'
resource :users,
only: [:edit, :update, :destroy],
controller: 'devise/registrations',
as: :user_registration do
get 'cancel'
end
rake routes output with the default devise modules:
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
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
You can override the "devise_scope" by placing it before the "devise_for".
devise_scope :user do
get "/users/sign_up", :to => "sites#index"
end
devise_for :users
Not sure if this is the best way but its my solution currently, as it just redirects back to the sign in page.
I liked #max's answer, but when trying to use it I ran into an error due to devise_mapping being nil.
I modified his solution slightly to one that seems to address the issue. It required wrapping the call to resource inside devise_scope.
devise_for :users, skip: [:registrations]
devise_scope :user do
resource :users,
only: [:edit, :update, :destroy],
controller: 'devise/registrations',
as: :user_registration do
get 'cancel'
end
end
Note that devise_scope expects the singular :user whereas resource expects the plural :users.
Do This in routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}, :skip => [:registrations]
as :user do
get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
put 'users' => 'devise/registrations#update', :as => 'user_registration'
end
devise_scope :user do
get "/sign_in", :to => "devise/sessions#new"
get "/sign_up", :to => "devise/registrations#new"
end
you will get an error now while you come to sign in page, to fix it.
Do this change in: app/views/devise/shared/_links.erb
<% if request.path != "/sign_in" %>
<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
<%= link_to "Sign up", new_registration_path(resource_name) %><br />
<% end -%>
<% end %>
I've found this to work well without messing with routes or adding application controller methods. My approach is to override the devise method. Add this to app/controllers/devise/registrations_controller.rb
I've omitted the other methods for brevity.
class Devise::RegistrationsController < DeviseController
...
# GET /resource/sign_up
def new
redirect_to root_path
end
....
end
Also to remove illusion that this path is still reachable from other views you might also want to remove this code from app/views/devise/shared/_links.erb
<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
<%= link_to "Sign up", new_registration_path(resource_name) %><br />
<% end -%>
For others in my case.
With devise (3.5.2).
I successfully removed the routes to signup, but kept the ones to edit the profile, with the following code.
#routes.rb
devise_for :users, skip: [:registrations]
as :user do
get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
put '/users(.:format)' => 'devise/registrations#update', as: 'user_registration'
patch '/users(.:format)' => 'devise/registrations#update'
end
Here's the slightly different route I went. It makes it so you don't have to override the devise/shared/_links.html.erb view.
In app/models/user.rb:
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
In config/routes.rb:
devise_for :users
devise_scope :user do
put 'users' => 'devise/registrations#update', as: 'user_registration'
get 'users/edit' => 'devise/registrations#edit', as: 'edit_user_registration'
delete 'users' => 'devise/registrations#destroy', as: 'registration'
end
Before:
$ rake routes | grep devise
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
After:
$ rake routes | grep devise
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
user_registration PUT /users(.:format) devise/registrations#update
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
registration DELETE /users(.:format) devise/registrations#destroy
Instead of searching for a hard solution. I used the below approaches.
Delete the sign_up form from page (path devise/registrations/new.html.erb) and replace it with custom info.
Redirect the incoming traffic to some other page. Like below in routes.rb
get "/users/sign_up", to: redirect('/')
post "/users/sign_up", to: redirect('/')
Make sure to write it before devise_for :users
I had the same issue and I found it a bit bad practise to redirect users from the registration page. So my solution is basically is not using :registrable at all.
What I did was to create a similar page like edit user details which looked like:
<%= form_tag(update_user_update_path, method: :post) do %>  
<br>
<%= label_tag(:currPassword, 'Current password:') %> <%= password_field_tag(:currPassword) %> <br>
<%= label_tag(:newPassword, 'New password:') %> <%= password_field_tag(:newPassword) %> <br>
<%= label_tag(:newPasswordConfirm, 'Confirm new password:') %> <%= password_field_tag(:newPasswordConfirm) %> <br>
<%= submit_tag('Update') %>
<% end %>
So this form submits into a new post end point that updates the password, which looks like:
def update
currPass = params['currPassword']
newPass1 = params['newPassword']
newPass2 = params['newPasswordConfirm']
currentUserParams = Hash.new()
currentUserParams[:current_password] = currPass
currentUserParams[:password] = newPass1
currentUserParams[:password_confirmation] = newPass2
#result = current_user.update_with_password(currentUserParams)
end
Later on you can use the #result in your view to tell the user whether the password is updated or not.
By changing the routes there are a whole bunch of other problems that come with that. The easiest method I have found is to do the following.
ApplicationController < ActionController::Base
before_action :dont_allow_user_self_registration
private
def dont_allow_user_self_registration
if ['devise/registrations','devise_invitable/registrations'].include?(params[:controller]) && ['new','create'].include?(params[:action])
redirect_to root_path
end
end
end
You could modify the devise gem itself. First, run this command to find the installed location of using:
gem which devise
Let's suppose the path is:
/usr/local/lib/ruby/gems/1.9.1/gems/devise-1.4.2/lib/devise
Then go to
/usr/local/lib/ruby/gems/1.9.1/gems/devise-1.4.2/lib/devise/lib/devise/rails and edit routes.rb in that directory. There is a method called def devise_registration(mapping, controllers) which you can modify to get rid of the new action. You can also completely remove the mappings for devise_registration

Rails3 Routing Error. :method => :delete, It will be "no route matches"

I want to call a destroy action. It is already define in controller.
The model that I want to destroy is nested resource. But route is existing in rake routes result.
new_content_model GET /contents/:content_id/model/new(.:format) {:controller=>"models", :action=>"new"}
edit_content_model GET /contents/:content_id/model/:id/edit(.:format) {:controller=>"models", :action=>"edit"}
content_model PUT /contents/:content_id/model/:id(.:format) {:controller=>"models", :action=>"update"}
DELETE /contents/:content_id/model/:id(.:format) {:controller=>"models", :action=>"destroy"}
And I call destroy action from this url
<%= link_to "destroy nested model", content_model_path( #content.id, #model.id ), :confirm => "are you sure?", :method => :delete %>
It is no route matches.
Routing Error
No route matches "/contents/1/model/1"`
Please tell me some solutions.
Already wrote <%= javascript_include_tag :all %> in layouts/application.html.erb
Rails 3 link_to (:method => :delete) not working
I solved this problem.
It was wrong that I deleted rails.js and prototype.js.
Thank you for your attending. Sorry for lack of information.