Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No route matches - 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]?

like image 721
Kali_89 Avatar asked Oct 20 '12 16:10

Kali_89


1 Answers

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
like image 118
Prakash Murthy Avatar answered Oct 13 '22 18:10

Prakash Murthy