2012-10-20 73 views
2

在Rails中設置我的第二個項目(首先不遵循教程),並遇到了我無法用我的路線解決的問題。一切工作,因爲我喜歡它與我的routes.rb文件,如:沒有路線匹配 - 導軌

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' 

除了我不能訪問我的動物!將/動物/ 1拋出一個錯誤: No route matches [GET] "/animal/1"

爲了解決這個問題,我改變了路線如下:

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' 

然而,當我嘗試並查看任何其他網頁上我的網站,我得到以下錯誤: No route matches {:action=>"show", :controller=>"animal"}

欲瞭解更多信息,請參閱我下面的控制器: (靜態頁面)

class StaticPagesController < ApplicationController 
    def help 
    end 

    def league 
    end 

    def contact 
    end 

    def about 
    end 

    def index 
    end 

    def show 
    end 
end 

動物控制器

class AnimalController < ApplicationController 
    def new 
    @animal = Animal.new 
    end 

def show 
    @animal = Animal.new 
end 
end 

而且,還有,我已經運行rake routes我得到:

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 

沒有任何人有任何想法,我怎樣才能讓我的網站回來,也讓自己看到的物體我的動物數據庫在URI /動物/ [:id]下?

回答

4

做,而不是它resources :animalsresources :animal

rake routes應顯示以下輸出:

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 
+0

嗨, 使你建議的修改,它使我的路由(或者至少是「動物'的)出現如上,我仍然看不到頁面。在查看任何其他頁面('localhost:3000 /'作爲主要示例)時出現與以前相同的錯誤,但在訪問'/ animals/1'時現在出現錯誤'未初始化的常量AnimalsController'。 –

+0

在動物控制器文件中,將控制器的名稱從「AnimalController」更改爲「AnimalsController」。 –

+0

另外,我強烈建議詳細閱讀[Ruby on Rails教程](http://ruby.railstutorial.org/ruby-on-rails-tutorial-book)的前幾章,以便深入瞭解Rails體系結構以及命名約定。 –