僵尸Rails 5级挑战5

时间:2011-05-17 10:54:13

标签: ruby-on-rails-3 rails-for-zombies

问题陈述是 创建命名路由。它应该生成一个类似'/ 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 个答案:

答案 0 :(得分:8)

试试这个

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

答案 1 :(得分: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

答案 2 :(得分:2)

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

TwitterForZombies::Application.routes.draw do
  get '/zombies/:name', to: 'zombies#index', :as => 'graveyard'
end

答案 3 :(得分: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