未初始化的常量Admin :: Admin

时间:2017-07-18 13:07:42

标签: ruby-on-rails ruby routes

使用Ruby:2.3.1p112和Rails:3.2.12

我正在尝试在我的控制器中调用一个演示方法。所以,在_form.html.erb我有:

<%= link_to 'Demo', "/admin/clinics/"+@clinic.id.to_s+"/demo" %>

在我的routes.rb

match "/admin" => "admin#index", :as => :admin

namespace :admin do
    resources :admin_users
    resources :health_plan_tables
    resources :health_aid_tables
    resources :clients
    resources :clinics
    resources :specialties
    resources :qualifications
    resources :profissionals
    resources :addresses
    resources :documents
    resources :banners
    root :to => 'banners#index'
    get 'logout' => 'devise/sessions#destroy'

    get 'clinics/:id/demo', to: 'admin/clinics#demo', as: 'demo'
end

我的clinics_controller.rb位于文件夹controllers/admin内,我只有:

def demo
    print "hello"
end 

因此,当我点击链接时,会显示错误消息Uninitialized constant Admin::Admin。 任何想法如何解决它?

2 个答案:

答案 0 :(得分:1)

根据错误日志,您正在寻找在admin / admin / clinics下命名的控制器(它表示在params的控制器部分)。

将底部路线更改为不包括管理员(它已经是命名空间,并且您实际上将其命名为两次):

get 'clinics/:id/demo', to: 'clinics#demo', as: 'demo'

这将路由到正确的控制器,admin / clinics,而不是admin / admin / clinics

答案 1 :(得分:1)

由于您已经在命名空间内定义了demo路由,因此无需指定admin/clinics#demo,只需要clinics#demo

namespace :admin do
    resources :admin_users
    resources :health_plan_tables
    resources :health_aid_tables
    resources :clients
    resources :clinics
    resources :specialties
    resources :qualifications
    resources :profissionals
    resources :addresses
    resources :documents
    resources :banners
    root :to => 'banners#index'
    get 'logout' => 'devise/sessions#destroy'

    get 'clinics/:id/demo', to: 'clinics#demo', as: 'demo'
end