使用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
。
任何想法如何解决它?
答案 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