Rails routes.rb无法按指定的路由运行

时间:2014-12-03 00:31:54

标签: ruby-on-rails

我将此代码链接到新角色:

<%= link_to "New Persona", genesis_path, class: "new-persona-button" %>

我的routes.rb文件:

Bazleyapp::Application.routes.draw do

  resources :users,    only: [:index, :create]
  resources :personas, only: [:index, :create]

  root                       'static_pages#home'

  get     'signup',                 to:  'users#new'
  get     '/:callsign',             to:  'users#show',     as: :user
  get     '/:callsign/settings',    to:  'users#edit',     as: :edit_user
  patch   '/:callsign',             to:  'users#update',   as: :update_user
  delete  '/:callsign/terminated',  to:  'users#destroy',  as: :destroy_user

  get     'genesis',                to:  'personas#new'
  get     '/:callsign',             to:  'personas#show',     as: :persona
  get     '/:callsign/settings',    to:  'personas#edit',     as: :edit_persona
  patch   '/:callsign',             to:  'personas#update',   as: :update_persona
  delete  '/:callsign/terminated',  to:  'personas#destroy',  as: :destroy_persona

end

单击新角色按钮会产生此错误:

Started GET "/genesis" for ::1 at 2014-12-03 00:25:38 +0000
Processing by UsersController#show as HTML
Parameters: {"callsign"=>"genesis"}
User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."callsign" = $1 LIMIT 1  [["callsign", "bazzer"]]
User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."callsign" = $1 LIMIT 1  [["callsign", "genesis"]]
Completed 500 Internal Server Error in 21ms

NoMethodError (undefined method `activated' for nil:NilClass):
app/controllers/users_controller.rb:17:in `show'

这意味着当它应该击中genesis_path时,它会触及user_path(用户#show)。但是我告诉它用<%= link_to "New Persona", genesis_path, class: "new-persona-button" %>来创造起源路径。很困惑。错误在哪里?

2 个答案:

答案 0 :(得分:1)

get 'genesis', to: 'personas#new'

之前

get '/:callsign', to: 'users#show', as: :user

您必须知道路线文件是从上到下读取的。

答案 1 :(得分:1)

我不认为你在这里有什么工作,因为你覆盖了几条路线。您可能需要考虑将它们放在单独的命名空间中,以使它们是唯一的。

示例:

Bazleyapp::Application.routes.draw do

  resources :users,    only: [:index, :create]
  resources :personas, only: [:index, :create]

  root                       'static_pages#home'

  namespace :users do
    get     'signup',                 to:  'users#new'
    get     '/:callsign',             to:  'users#show',     as: :user
    get     '/:callsign/settings',    to:  'users#edit',     as: :edit_user
    patch   '/:callsign',             to:  'users#update',   as: :update_user
    delete  '/:callsign/terminated',  to:  'users#destroy',  as: :destroy_user
  end

  namespace :personas do
    get     'genesis',                to:  'personas#new'
    get     '/:callsign',             to:  'personas#show',     as: :persona
    get     '/:callsign/settings',    to:  'personas#edit',     as: :edit_persona
    patch   '/:callsign',             to:  'personas#update',   as: :update_persona
    delete  '/:callsign/terminated',  to:  'personas#destroy',  as: :destroy_persona
  end
end

这样就可以分开这些路线的路径。

您还可以向资源添加块,而不必将其拆分。例如:

resources :users,    only: [:index, :create] do
  get     'signup',                 to:  'users#new'
  get     '/:callsign',             to:  'users#show',     as: :user
  get     '/:callsign/settings',    to:  'users#edit',     as: :edit_user
  patch   '/:callsign',             to:  'users#update',   as: :update_user
  delete  '/:callsign/terminated',  to:  'users#destroy',  as: :destroy_user
end