Getting a routing error from certain pages - ruby-on-rails

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.

Related

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!

Implementing :except in the routes for devise_for

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.

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.

Ruby On Rails Application.html.haml

I am using Ruby On Rails 2.3.5 with haml version 2.2.21 (in my environment.rb file) And all the links I generate in views (using haml) adds the link itself at the end.
Ex :
%li
=link_to 'sign up', {:controller=> 'users' , :action=> 'signup'}
Displays
sign up(users/signup)
Anyone knows why ?
you should just be able to do:
=link_to 'Sign Up', user_registration_path
For future reference, you can run the command
rake routes
To get a list of all the named routes in your application:
$ 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"}
user_password PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
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"}
user_registration PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
user_registration DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
The route name is listed in the leftmost column. You can then append '_path' to any of those to access that path.

Rails Routes Namespaces and form_for

I have namespace in my routes.rb
namespace :businesses do
resources :registration
end
My controller is in a subdirectory businesses/registration_controller.
def new
#business = Business.new
end
In my view, I want to do this form_for #business do |f| ... but I am getting the following error:
No route matches {:controller=>"businesses", :action=>"create"}
Restarted the server and I'm also getting this:
undefined methodbusinesses_path' for #<#:0x10339bb20>`
Here are my rake routes:
home_index GET /home/index(.:format) {:action=>"index", :controller=>"home"}
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"}
user_password 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"}
user_registration PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
user_registration DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
users GET /users(.:format) {:action=>"index", :controller=>"users"}
users POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
user PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
user DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
full_tree_admin_categories GET /admin/categories/full_tree(.:format) {:action=>"full_tree", :controller=>"admin/categories"}
admin_categories GET /admin/categories(.:format) {:action=>"index", :controller=>"admin/categories"}
admin_categories POST /admin/categories(.:format) {:action=>"create", :controller=>"admin/categories"}
new_admin_category GET /admin/categories/new(.:format) {:action=>"new", :controller=>"admin/categories"}
edit_admin_category GET /admin/categories/:id/edit(.:format) {:action=>"edit", :controller=>"admin/categories"}
admin_category GET /admin/categories/:id(.:format) {:action=>"show", :controller=>"admin/categories"}
admin_category PUT /admin/categories/:id(.:format) {:action=>"update", :controller=>"admin/categories"}
admin_category DELETE /admin/categories/:id(.:format) {:action=>"destroy", :controller=>"admin/categories"}
businesses_registration_index GET /businesses/registration(.:format) {:action=>"index", :controller=>"businesses/registration"}
businesses_registration_index POST /businesses/registration(.:format) {:action=>"create", :controller=>"businesses/registration"}
new_businesses_registration GET /businesses/registration/new(.:format) {:action=>"new", :controller=>"businesses/registration"}
edit_businesses_registration GET /businesses/registration/:id/edit(.:format) {:action=>"edit", :controller=>"businesses/registration"}
businesses_registration GET /businesses/registration/:id(.:format) {:action=>"show", :controller=>"businesses/registration"}
businesses_registration PUT /businesses/registration/:id(.:format) {:action=>"update", :controller=>"businesses/registration"}
businesses_registration DELETE /businesses/registration/:id(.:format) {:action=>"destroy", :controller=>"businesses/registration"}
root /(.:format) {:action=>"index", :controller=>"home"}
If you have namespaced routes the best way is:
class Admin::BusinessesController < ApplicationController
def new
#business = Business.new
end
end
in routes.rb:
namespace :admin do
resources :businesses
end
In view:
form_for [:admin, #business] do |f|...
The Docs: http://guides.rubyonrails.org/form_helpers.html section 2.3.1 Dealing with Namespaces
Regarding your case:
In routes.rb everything is o'k. In the view you should write url explicitly because you have variable in controller other than controller name:
form_for #business, :url => business_registration_path do |f|...
I suppose that in businesses/registration_controller you have something like this:
class Businesses::RegistrationController < ApplicationController
def new
#business = Business.new
end
end
And one remark: I wouldn't create registration_controller for registering a new business. I think that keeping business related actions in business_controller is much clearer.
Actually, I think there is a better solution.
form_for [:admin, #business]
the issue with giving a url is that if you abstract the form out as a partial view, you'll need to deal with "create" and "update" situations. They points to different urls, and ends up with providing the #url in controller.

Resources