Implementing :except in the routes for devise_for - ruby-on-rails

By using devise for user_auth it sets up some default routes (e.g. /user/edit and /user/sign_in). In routes.rb I have
devise_scope :user do
get 'signin' => 'devise/sessions#new'
etc.
end
The problem is that both /signin and /user/sign_in work to link to the sign in page. I want to so how make the /user/sign_in link inactive meaning it gives a 404 error when navigating to that page.
My rake routes is:
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=>"registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"registrations"}
signin GET /signin(.:format) {:action=>"new", :controller=>"devise/sessions"}
signout GET /signout(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
signup GET /signup(.:format) {:action=>"new", :controller=>"devise/registrations"}
command GET /command(.:format) {:action=>"edit", :controller=>"devise/registrations"}

Use devise_for to change the path and names of the default urls
devise_for :users, :path => '', :path_names => { :sign_in => 'signin', :sign_out => 'signout', :sign_up => 'signup' }
I would recommend leaving the path names as sign_in, sign_out, and sign_up since signin looks like gibberish.
Also, selectively editing these defaults will probably just make your routes more confusing in the long run. So, unless there is a great reason to overwrite the defaults, you should go with what devise recommends.

Related

Getting a routing error from certain pages

I have this route
profile GET /contacts/:id(.:format) {:controller=>"my_devise/contacts", :action=>"profile"}
This is my controllers/application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :get_current_user
def get_current_user
#current_user = current_user
end
end
If i have this link in my view/layouts/application.html.erb file <%= link_to "Profile", profile_path(#current_user) %> on the url http://localhost:3000/contacts/1, i have no errors, but if i try to hit the url http://localhost:3000/contacts, I get the error below
Routing Error
No route matches {:controller=>"my_devise/contacts", :action=>"profile"}
If i remove the link in my application.html.erb file and hit http://localhost:3000/contacts, the error goes away.
Why would that link cause this error?
EDIT
Full routes file
$ rake routes
users_sign_out GET /users/sign_out(.:format) {:controller=>"devise/sessions", :action=>"destroy"}
users_sign_in GET /users/sign_in(.:format) {:controller=>"my_devise/sessions", :action=>"new"}
home GET /home(.:format) {:action=>"index", :controller=>"my_devise/sessions"}
contacts GET /contacts(.:format) {:action=>"list", :controller=>"my_devise/contacts"}
profile GET /contacts/:id(.:format) {:controller=>"my_devise/contacts", :action=>"profile"}
edit_profile GET /contacts/:id/edit(.:format) {:controller=>"my_devise/contacts", :action=>"edit"}
POST /contacts/:id/edit(.:format) {:controller=>"my_devise/contacts", :action=>"update_user"}
more GET /more/:id(.:format) {:controller=>"my_devise/contacts", :action=>"more"}
POST /home(.:format) {:action=>"create_new_user", :controller=>"my_devise/sessions"}
users_sign_up GET /users/sign_up(.:format) {:controller=>"my_devise/registrations", :action=>"new"}
POST /users/sign_up(.:format) {:controller=>"my_devise/registrations", :action=>"new"}
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"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"my_devise/sessions"}
POST /users/sign_in(.:format) {:action=>"create", :controller=>"my_devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"my_devise/sessions"}
POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
GET /users/cancel(.:format) {:action=>"cancel", :controller=>"my_devise/registrations"}
POST /users(.:format) {:action=>"create", :controller=>"my_devise/registrations"}
GET /users/sign_up(.:format) {:action=>"new", :controller=>"my_devise/registrations"}
GET /users/edit(.:format) {:action=>"edit", :controller=>"my_devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"my_devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"my_devise/registrations"}
home_index GET /home/index(.:format) {:controller=>"home", :action=>"index"}
root / {:controller=>"home", :action=>"index"}
root / {:controller=>"home", :action=>"index"}
To answer your actual question, it's because you don't have a route for that (/contacts - note no id). Your route is /contacts/:id(.:format) - the format is optional, but the id is not. You'll need to make the id optional, too, or create another route.
The routes is like this
contacts GET /contacts(.:format) {:action=>"list", :controller=>"my_devise/contacts"}
Here we have to give the format also. If we give contacts it will throw routing error. So please enter the format also.

Routing error with devise {:controller=>"devise/static", :action=>"about"}

I got a problem with devise. Every time I try to call an Url that should be handled by devise (e.g. http://localhost:3000/users/sign_up) I end up with the following error:
No route matches {:controller=>"devise/static", :action=>"about"}
Hopefully somebody can help me!
routes.rb
devise_for :users
get "pages/contact"
get "pages/imprint"
get "pages/about"
root :to => "pages#about"
What I did:
Added gem 'devise' to the gemfile
bundle install
rails generate devise:install
rails generate devise User
rake db:migrate
rake routes
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"}
pages_contact GET /pages/contact(.:format) {:controller=>"pages", :action=>"contact"}
pages_imprint GET /pages/imprint(.:format) {:controller=>"pages", :action=>"imprint"}
pages_about GET /pages/about(.:format) {:controller=>"pages", :action=>"about"}
root / {:controller=>"pages", :action=>"about"}
Rails Version:
Rails 3.1.3
PagesController
class PagesController < ApplicationController
def contact
end
def imprint
end
def about
end
end
The error for me was fixed when I changed my link_to methods.
In my header I had:
<%= link_to "Info", :controller => :info %>
and when I switched it to:
<%= link_to "Info", "/info" %>
the error was gone!

Ruby on Rails devise http://0.0.0.0:3000/users/sign_out gets routing error

routes.rb =>
Sendemail::Application.routes.draw do
devise_for :users
get "user/index"
get "home/index"
root :to => 'home#index'
end
rake routes =>
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"}
user_confirmation POST /users/confirmation(.:format) {:action=>"create", :controller=>"devise/confirmations"}
new_user_confirmation GET /users/confirmation/new(.:format) {:action=>"new", :controller=>"devise/confirmations"}
GET /users/confirmation(.:format) {:action=>"show", :controller=>"devise/confirmations"}
user_index GET /user/index(.:format) {:controller=>"user", :action=>"index"}
home_index GET /home/index(.:format) {:controller=>"home", :action=>"index"}
root / {:controller=>"home", :action=>"index"}
When i type rails server i get this message every time =>
/home/user1/.rvm/gems/ruby-1.9.2-p180/gems/rack-1.3.4/lib/rack/backports/uri/common_192.rb:53: warning: already initialized constant WFKV
For "http://0.0.0.0:3000/users/sign_out"
Routing Error
No route matches [GET] "/users/sign_out"
What should i do to fix this problem ?
Notice how your routes say /users/sign_out is a DELETE request. Most browsers do not make DELETE requests by default. Just going to that URL would be a GET request to /users/sign_out.
This how to add sign out links should help you get it working.
Add :method => :delete to your signout link.

Rails 3 Devise - Getting " No route matches "/users/sign_out" "

No route matches "/users/sign_out" When I am logged in. I just followed ryan bates tutorial to get devise working. My rake routes looks like this.
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"}
root /(.:format) {:controller=>"welcome", :action=>"index"}
Thanks in advance.
Routes look correct. Your sign out link should look like this:
<%= link_to('Logout', destroy_user_session_path, :method => :delete) %>
I assume the other answer solved your problem. If you want to know why, check out the section in this setup guide for rails 3.1 with devise. Basically, when you try to HTTP GET the sign out route, it doesn't exist because it's only setup for HTTP DELETE. You can see this in the second column of the routes you pasted in the question. Probably your links were missing the
:method => :delete
Also in that tutorial, you can see how to setup devise to use the GET method when it is in test mode. Change /config/initializers/devise.rb as follows:
# The default HTTP method used to sign out a resource. Default is :delete.
config.sign_out_via = Rails.env.test? ? :get : :delete

Why does devise put /devise/ in front of every link_to anchor?

I just generated all the views for devise, and I'm starting to customize the login screen. It works great except for all the links that are generated on the sign in page start with "/devise".
Why is it doing that? Seems like odd default behaviour
How do I stop it from adding /devise to every link_to()?
My routes file:
devise_for :users
get "/webpages/:page" => "webpages#show", :as => :show_webpage
root :to => "webpages#index"
My 'rake routes'
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 GET /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"}
show_webpage GET /webpages/:page(.:format) {:controller=>"webpages", :action=>"show"}
root /(.:format) {:controller=>"webpages", :action=>"index"}
The error message I get when I try and render a page that comes from the devise controller:
ActionController::RoutingError in Devise/sessions#new
Showing /home/egervari/Projects/training/app/views/layouts/application.html.erb where line #21 raised:
No route matches {:controller=>"devise/webpages", :action=>"show", :page=>"tour"}
Extracted source (around line #21):
18: </a>
19: </li>
20: <li>
21: <%= link_to("Tour", :controller => "webpages", :action => "show", :page => "tour") %>
22: </li>
23: <li>
24: <%= link_to("Why Use Us?", :controller => "webpages", :action => "show", :page => "why") %>
As you can see above, it's trying to add "devise/" to my link. This is not what I want at all.
I figured it out finally.
<li><%= link_to("Terms and Use", :controller => "/webpages", :action => "show", :page => "terms") %> |</li>
<li><%= link_to("Privacy Policy", :controller => "/webpages", :action => "show", :page => "privacy") %> |</li>
Basically what I did was put "/webpages" instead of "webpages" to tell rails that these controllers were not under the "devise" namespace or parent directory.
Is this the appropriate fix? Is there a simpler solution?
In newer versions of Rails you can do:
<%= link_to "Privacy Policy", show_webpage_path(:page => 'privacy') %>
You just append a _path onto the named route that you see when you do a 'rake routes'. Appending _url to the named route will give you the URL string, BTW. Which can be useful.
ian.
All links/forms targeted to devise are expected to start with "/devise". See the routes generated by devise below. Why do you want change this behavior? Is it not working? Or do you need/want to customize the devise controllers?
new_user_session GET /users/login(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/login(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session GET /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/register(.: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"}
user_confirmation POST /users/confirmation(.:format) {:action=>"create", :controller=>"devise/confirmations"}
new_user_confirmation GET /users/confirmation/new(.:format) {:action=>"new", :controller=>"devise/confirmations"}
GET /users/confirmation(.:format) {:action=>"show", :controller=>"devise/confirmations"}

Resources