没有路由匹配-Rails。

5

我正在为我的第二个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'

除了我无法访问我的动物!前往/animal/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 /animal/[:id]下查看我的动物数据库中的对象吗?请注意,不要删除HTML标签。
1个回答

5

将其改为资源 :animals而不是资源 :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

你好,我已经按照你的建议进行了更改,现在我的路由(至少是“animals”)看起来像上面那样,但我仍然无法看到页面。当查看任何其他页面时(以localhost:3000/为主要示例),与之前相同的错误出现,但现在访问/animals/1时出现“未初始化常量AnimalsController”的错误。 - Kali_89
在动物控制器文件中,将控制器的名称从AnimalController更改为AnimalsController - Prakash Murthy
此外,我强烈建议您详细阅读Ruby on Rails教程的前几章,以便您对Rails架构以及命名约定有一个扎实的理解。 - Prakash Murthy
即使我已经更改了动物控制器的名称,但仍然无法在网站的任何其他页面上获得任何内容。我一遍又一遍地重新阅读了教程(那是第一个应用程序,我一直尽可能地按照它来操作这个应用程序)。我会继续阅读! - Kali_89

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接