No route matches - Rails - ruby-on-rails

Setting up my second project in Rails (first not following a tutorial) and have run into a problem I can't solve with my routes. Everything works as I'd like it to with my routes.rb file like:
AnimalApp::Application.routes.draw do
root to: 'static_pages#index'
# resources :animal
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
match '/league', to: 'static_pages#league'
match '/animal', to: 'animal#new'
Except I can't access my animals! Going to /animal/1 throws an error :
No route matches [GET] "/animal/1"
To fix this I changed my routes as follows:
AnimalApp::Application.routes.draw do
root to: 'static_pages#index'
resources :animal
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
match '/league', to: 'static_pages#league'
#match '/animal', to: 'animal#new'
However, when I try and view any of the other pages on my site I get the following error:
No route matches {:action=>"show", :controller=>"animal"}
For more information see below my controllers:
(static pages)
class StaticPagesController < ApplicationController
def help
end
def league
end
def contact
end
def about
end
def index
end
def show
end
end
animals controller
class AnimalController < ApplicationController
def new
#animal = Animal.new
end
def show
#animal = Animal.new
end
end
And, also, I've run rake routes I get:
animal_index GET /animal(.:format) animal#index
POST /animal(.:format) animal#create
new_animal GET /animal/new(.:format) animal#new
edit_animal GET /animal/:id/edit(.:format) animal#edit
animal GET /animal/:id(.:format) animal#show
PUT /animal/:id(.:format) animal#update
DELETE /animal/:id(.:format) animal#destroy
root / static_pages#index
help /help(.:format) static_pages#help
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
league /league(.:format) static_pages#league
/animal(.:format) animal#new
Does anybody have any idea how I can get my site back and also allow myself to see objects in my animal database under the URI /animal/[:id]?

Make it resources :animals instead of resources :animal
rake routes should show the following output:
animals GET /animals(.:format) animals#index
POST /animals(.:format) animals#create
new_animal GET /animals/new(.:format) animals#new
edit_animal GET /animals/:id/edit(.:format) animals#edit
animal GET /animals/:id(.:format) animals#show
PUT /animals/:id(.:format) animals#update
DELETE /animals/:id(.:format) animals#destroy

Related

Rails exception: No route matches [GET] "/clock_events/1/clock_in"

It used to work but after some changes, Action Controller is catching an exception
Routing Error: No route matches [GET] "/clock_events/1/clock_in"
routes.rb file:
Rails.application.routes.draw do
root to: 'clock_events#index'
get '/register', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
get '/logout', to: 'sessions#destroy'
resources :clock_events, except: [:destroy] do
member do
post 'clock_in', to: 'clocks#clock_in'
post 'clock_out', to: 'clocks#clock_out'
end
end
resources :users, except: [:destroy]
end
You've defined clock_in with the post http verb, here:
resources :clock_events, except: [:destroy] do
member do
post 'clock_in', to: 'clocks#clock_in'
post 'clock_out', to: 'clocks#clock_out'
end
end
But, you're trying to use the get verb, as indicated here:
Routing Error: No route matches [GET] "/clock_events/1/clock_in"
You need to either change your path to use the get verb:
resources :clock_events, except: [:destroy] do
member do
get 'clock_in', to: 'clocks#clock_in'
post 'clock_out', to: 'clocks#clock_out'
end
end
Or modify your link (or whatever) to use the post method.
Also, your clock_in and clock_out actions are called on the clocks controller, not the clock_events controller, as indicated by your to: directive:
resources :clock_events, except: [:destroy] do
member do
post 'clock_in', to: 'clocks#clock_in'
post 'clock_out', to: 'clocks#clock_out'
end
end
Are you sure you don't want to use the ClockEventsController? If so, you could do:
resources :clock_events, except: [:destroy] do
member do
post :clock_in
post :clock_out
end
end
In which case you would get:
clock_in_clock_event POST /clock_events/:id/clock_in(.:format) clock_events#clock_in
clock_out_clock_event POST /clock_events/:id/clock_out(.:format) clock_events#clock_out
clock_events GET /clock_events(.:format) clock_events#index
POST /clock_events(.:format) clock_events#create
new_clock_event GET /clock_events/new(.:format) clock_events#new
edit_clock_event GET /clock_events/:id/edit(.:format) clock_events#edit
clock_event GET /clock_events/:id(.:format) clock_events#show
PATCH /clock_events/:id(.:format) clock_events#update
PUT /clock_events/:id(.:format) clock_events#update

Ruby on Rails Devise id='sign up'

So a few people have had this problem, but none of their code actually matches mine so their solutions aren't working.
Basically, whenever I try to access /users/:id it passes in the params correctly {'id' => '1'}, but then the page errors out on 'no user with id='sign_out'
My routes:
Rails.application.routes.draw do
devise_for :users, path: 'devise'
get 'sessions/new'
get 'intro', to: 'index#intro'
root 'index#intro'
authenticated :user do
root 'index#homepage', as: :authenticated_root
end
devise_scope :user do
get '/start', to: 'devise/sessions#new', as: "login"
get '/devise/sign_out', to: 'devise/sessions#destroy', as: "logout"
get '/signup', to: 'users#new', as: "signup"
end
get '/forgotPassword', to: 'index#forgotPassword'
get '/homepage', to: 'index#homepage'
get '/meetUs', to: 'index#meetUs'
get '/makeSuggestion', to: 'index#makeSuggestion'
get 'profile', to: 'index#profile'
resources :suggestions
resources :rewards
resources :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
Code in controller erroring out:
# GET /users/1
# GET /users/1.json
def show
#users = User.find(params[:id])
end
I'm pretty sure the issue has something to do with the routes thinking /users is both a resource of mine and of devise. But I don't know how to tell it to use the default route rails made as opposed to the one devise made.
If anyone could help, and try and explain why it's happening, that'd be great. Thanks in advance!
edit: Picks of the error and trace

I get error "uninitialized constant ContactController"

routes.rb file is
Rails.application.routes.draw do
root 'pages#home'
match '/contact', to: 'pages#contact', via: 'get'
match '/home', to: 'contact#pages', via: 'get'
pages_controller is
class PagesController < ApplicationController
def home
end
def contact
end
end
I get routing error saying "uninitialized constant ContactController". Does anyone know how to fix this?
Do Change your routes.rb file
Rails.application.routes.draw do
root 'pages#home'
match '/contact', to: 'pages#contact', via: :get
match '/home', to: 'pages#home', via: :get
end
Would work!!!
In Rails 4 with match methods must specify HTTP method otherwise would get RuntimeError "You should not use the match method in your router without specifying an HTTP method. (RuntimeError)"
match is deprecated. Try
root 'pages#home'
get '/contact', to: 'pages#contact'
get '/home', to: 'pages#home'

Is there a method to change URL using namespaces?

I'm trying to change the URL name.
This is my routes.rb:
namespace :user_management do
resources :user do
collection do
get 'main'
end
end
end
match ':controller(/:action(/:id(.:format)))', :via => [:get, :post]
Rake routes:
Prefix Verb URI Pattern Controller#Action
root GET / user_management/login#login
user_main_user_management_user_index GET /user_management/user/main(.:format) user_management/user#main
user_management_user_index GET /user_management/user(.:format) user_management/user#index
POST /user_management/user(.:format) user_management/user#create
new_user_management_user GET /user_management/user/new(.:format) user_management/user#new
edit_user_management_user GET /user_management/user/:id/edit(.:format) user_management/user#edit
user_management_user GET /user_management/user/:id(.:format) user_management/user#show
PATCH /user_management/user/:id(.:format) user_management/user#update
PUT /user_management/user/:id(.:format) user_management/user#update
DELETE /user_management/user/:id(.:format) user_management/user#destroy
GET|POST /:controller(/:action(/:id(.:format))) :controller#:action
My URL is:
localhost/user_management/user/main
And I want:
localhost/user_main
I tried this but it is not working:
namespace :user_management do
resources :user do
collection do
get 'main', as: :user_main
end
end
end
I tried this but is not working either:
namespace :user_management do
resources :user do
collection do
get '/user_main', as: "user_management/user#main
end
end
end
try
match "user_admin", :to => "user_management/user#main"
you can do like in example:
get 'exit', to: 'sessions#destroy', as: :logout # url => /logout
FROM RAILS ROUTING

Refactoring Rails Routes to still pass testing

I can't get this to pass:
%w[home help about contact].each do |page|
get :controller => 'static_pages', :action => page
end
I was trying to refactor this code:
get 'static_pages/home'
get 'static_pages/help'
get 'static_pages/about'
get 'static_pages/contact'
Please help.
static_pages = %w(home help about contact).map {|p| p.to_sym}
resources :static_pages, only: static_pages do
static_pages.each do |page|
get page, on: :collection
end
end
render
$ rake routes
home_static_pages GET /static_pages/home(.:format) static_pages#home
help_static_pages GET /static_pages/help(.:format) static_pages#help
about_static_pages GET /static_pages/about(.:format) static_pages#about
contact_static_pages GET /static_pages/contact(.:format) static_pages#contact
use only: static_pages to not generate CRUD routes.
I guess if you don't like the original code of:
root to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
and you want to get the same routes, you could go with a simple loop like:
root to: 'static_pages#home'
%w(help about contact).each do |page|
get "#{page}" => "static_pages##{page}"
end
Though, to me this doesn't really seem like enough of a refactor to justify it.
Edit
To fix the errors on your code, use:
%w(home help about contact).each do |page|
get "/#{page}", :controller => 'static_pages', :action => page
end
Check the api for match for valid arguments to get.

Resources