在轨道3中没有模型的控制器的路由

时间:2021-10-03 21:09:54

I have a controller named BaseController that inherits from ApplicationController whitout a model associated but it has ping method that just respond with a message to inform that everything is OK.

我有一个名为BaseController的控制器,它从ApplicationController继承了一个关联的模型,但它有一个ping方法,它只响应一条消息来通知一切正常。

I'm trying to call the action ping through the BaseController setting this in my routes.rb file:

我试图通过我的routes.rb文件中的BaseController设置调用动作ping:

namespace :api, defaults: { format: 'json' } do   
  match '/ping' => 'base#ping' 
end

But it always give me an NameError uninitialized constant Base. I suppose it's trying to find a model called Base which doesn't exist so, I don't know how to set to the correct route to my controller.

但它总是给我一个NameError未初始化的常量Base。我想它正在尝试找到一个名为Base的模型,它不存在,所以我不知道如何设置到我的控制器的正确路径。

The content of my BaseController is the following:

我的BaseController的内容如下:

class Api::BaseController < ApplicationController
   load_and_authorize_resource
   respond_to :json

   def ping
      respond_with({ :status => 'OK' })
   end
end

As extra information: BaseController is just a parent controller for other controllers to inherit. The others are resourceful controllers and have models associated

作为额外信息:BaseController只是其他控制器继承的父控制器。其他是资源丰富的控制器,并具有相关的模型

Thanks.

3 个解决方案

#1


9  

When you put a namespace around a route, it will look for the controller within that namespace.

当您在路由周围放置命名空间时,它将在该命名空间中查找控制器。

So in you case, it will be looking for a controller called Api::BaseController, which normally would be stored in app/controllers/api/base_controller.rb. Is this how your controller is set up?

所以在你的情况下,它将寻找一个名为Api :: BaseController的控制器,它通常存储在app / controllers / api / base_controller.rb中。这是你的控制器的设置方式吗?

See here for more details: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

有关更多详细信息,请参见此处:http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

EDIT:

I don't think its not finding the controller that's the problem. The error is being caused because you are calling load_and_authorize_resource in the controller. CanCan uses the controller name to attempt to load the resource.

我不认为它没有找到控制器是问题。由于您在控制器中调用load_and_authorize_resource,因此导致错误。 CanCan使用控制器名称来尝试加载资源。

If there is no model for the controller, make the call authorize_resource :class => false.

如果控制器没有模型,则调用authorize_resource:class => false。

See the bottom of this page for more details.

有关详细信息,请参阅本页底部。

#2


0  

Please try this:

请试试这个:

Add this in your routes.rb

在您的routes.rb中添加此项

resources :base

#3


0  

Try this in your routes.rb map.resources :base, :collection=> {:ping=>:get}

在您的路由中尝试这个.rb map.resources:base,:collection => {:ping =>:get}

#1


9  

When you put a namespace around a route, it will look for the controller within that namespace.

当您在路由周围放置命名空间时,它将在该命名空间中查找控制器。

So in you case, it will be looking for a controller called Api::BaseController, which normally would be stored in app/controllers/api/base_controller.rb. Is this how your controller is set up?

所以在你的情况下,它将寻找一个名为Api :: BaseController的控制器,它通常存储在app / controllers / api / base_controller.rb中。这是你的控制器的设置方式吗?

See here for more details: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

有关更多详细信息,请参见此处:http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

EDIT:

I don't think its not finding the controller that's the problem. The error is being caused because you are calling load_and_authorize_resource in the controller. CanCan uses the controller name to attempt to load the resource.

我不认为它没有找到控制器是问题。由于您在控制器中调用load_and_authorize_resource,因此导致错误。 CanCan使用控制器名称来尝试加载资源。

If there is no model for the controller, make the call authorize_resource :class => false.

如果控制器没有模型,则调用authorize_resource:class => false。

See the bottom of this page for more details.

有关详细信息,请参阅本页底部。

#2


0  

Please try this:

请试试这个:

Add this in your routes.rb

在您的routes.rb中添加此项

resources :base

#3


0  

Try this in your routes.rb map.resources :base, :collection=> {:ping=>:get}

在您的路由中尝试这个.rb map.resources:base,:collection => {:ping =>:get}