6

问题陈述是创建命名路由。它应该生成一个类似 '/zombies/:name' 的路径,其中 :name 是一个参数,并指向 ZombiesController 中的索引操作。将路线命名为“墓地”

资源就是资源

zombies
id  name    graveyard
1   Ash     Glen Haven Memorial Cemetary
2   Bob     Chapel Hill Cemetary
3   Jim     My Fathers Basement

我的解决方案是

TwitterForZombies::Application.routes.draw do
  match ':name' => 'Zombies#index', :as => 'graveyard'
end

我也试过

TwitterForZombies::Application.routes.draw do
      match ':name' => 'Zombie#index', :as => 'graveyard'
    end

我在这两种情况下得到的错误是

Sorry, Try Again
Did not route to ZombiesController index action with :name parameter

我究竟做错了什么??

4

4 回答 4

8

试试这个

match '/zombies/:name' => 'zombies#index', :as => 'graveyard'
于 2011-05-17T11:14:08.250 回答
8

试试这个:

match '/zombies/:name',:to=> 'zombies#index', :as => 'graveyard'

RailsForZombies::Application.routes.draw do
    resources :zombie
    match '/zombies/:name',:to=> 'Zombies#index', :as => 'graveyard'
end
于 2011-05-17T11:36:28.923 回答
2

Rails for Zombies 已针对 Rails 4 进行了更新。match方法已被弃用。Rails 现在需要指定响应哪个 HTTP 动词(例如get )。对于 Rails for Zombies 更新的 5 级挑战 5,这是一种可能的解决方案:

TwitterForZombies::Application.routes.draw do
  get '/zombies/:name', to: 'zombies#index', :as => 'graveyard'
end
于 2014-02-21T19:51:26.077 回答
1

这对我有用

RailsForZombies::Application.routes.draw do
  match 'zombies/:name' => 'Zombies#index' , :as => "graveyard"
  #match ':name' => 'Zombie#index', :as => 'graveyard'
  #match 'zombies/:name',:to=> 'zombies#index', :as => 'graveyard'
end
于 2012-06-22T16:47:09.030 回答