【问题标题】:No route matches - Rails没有路线匹配 - Rails
【发布时间】:2012-10-11 00:50:36
【问题描述】:

在 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] 下的对象?

【问题讨论】:

    标签: ruby-on-rails ruby routes ruby-on-rails-3.2


    【解决方案1】:

    改为resources :animals 而不是resources :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
    

    【讨论】:

    • 嗨,已经做出了您建议的更改,它使我的路线(或至少是“动物”路线)如上所示,我仍然看不到页面。查看任何其他页面时出现与以前相同的错误(localhost:3000/ 作为主要示例),但现在访问/animals/1 时出现错误uninitialized constant AnimalsController
    • 在动物控制器文件中,将控制器的名称从AnimalController 更改为'AnimalsController`。
    • 另外,我强烈建议您详细阅读Ruby on Rails Tutorial 的前几章,以便您对 Rails 架构以及命名约定有一个扎实的了解。
    • 即使这样做(动物控制器的重命名)我仍然没有在网站上的任何其他页面上得到任何东西。我一次又一次地重读了教程(那是第一个应用程序,我一直在尽可能密切地关注它)。我会继续阅读!
    猜你喜欢
    • 2017-07-13
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 2013-03-14
    相关资源
    最近更新 更多