Rails 路由和控制器模块 - 命名空间? [英] Rails routing and controller modules -namespacing?

查看:46
本文介绍了Rails 路由和控制器模块 - 命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为我的控制器创建一个模块以及让我的路由指向控制器中的那个模块时遇到问题.

I have trouble creating a module for my controller, and getting my routes to point to that module within the controller.

出现此错误:

Routing Error
uninitialized constant Api::Fb

所以,我的路线是这样设置的:

So, this is how my routes are set up:

namespace :api do
  namespace :fb do
    post :login
    resources :my_lists do
      resources :my_wishes
    end
  end
end

在我的 fb_controller 中,我想包含可以为我提供如下路径的模块:

In my fb_controller i want to include modules that will give me paths like this:

/api/fb/my_lists

这是我的一些 fb_controller:

This is some of my fb_controller:

class Api::FbController < ApplicationController
  skip_before_filter :authenticate_user!, :only => [:login]

  include MyLists # <-- This is where i want to include the /my_lists
                  # namespace(currently not working, and gives me error 
                  # mentioned above)

  def login
    #loads of logic
  end
end

MyLists.rb 文件(我在其中定义了一个模块)与 fb_controller.rb 位于同一目录中.

The MyLists.rb file(where i define a module) is in the same directory as the fb_controller.rb.

如何让命名空间指向我在 fb_controller 中的模块,比如/api/fb/my_lists ?

How can i get the namespacing to point to my module inside of the fb_controller, like /api/fb/my_lists ?

推荐答案

您设置的命名空间正在寻找看起来像这样的控制器类

The namespace you have set up is looking for a controller class that looks like this

class Api::Fb::MyListsController

如果你想要一个看起来像 /api/fb/my_lists 的路由,但你仍然想使用 FbController 而不是 MyListsController 你需要把你的路由设置成这样

If you want to have a route that looks like /api/fb/my_lists but you want to still use the FbController instead of having a MyListsController you need to set up your routes to look like this

namespace :api do
  scope "/fb" do
    resources :my_lists, :controller => 'fb'
  end
end

在我看来,在你的 FbController 中包含一个模块 MyLists 似乎有点尴尬.

In my opinion, instead of including a module MyLists in your FbController seems kind of awkward.

我可能会做的是有一个带有通用 FbController 的模块 FB 然后有 MyListsController <FbController.无论如何,这超出了您的问题范围.

What I would probably do is have a module FB with a generic FbController then have MyListsController < FbController. Anyway, this is beyond the scope of your question.

以上应该可以满足您的需求.

The above should answer for your needs.

编辑

根据您的评论,以及我对您尝试这样做的假设,这是一个小例子:

From your comments, and my assumptions on what you're trying to do this is a small example:

config/routes.rb

namespace :api do
  scope "/fb" do
    post "login" => "fb#login"
    # some fb controller specific routes
    resources :my_lists
  end
end

api/fb/fb_controller.rb

class Api::FbController < ApiController
  # some facebook specific logic like authorization and such.
  def login
  end
end

api/fb/my_lists_controller.rb

class Api::MyListsController < Api::FbController
  def create
    # Here the controller should gather the parameters and call the model's create
  end
end

现在,如果您只想创建一个 MyList 对象,那么您可以直接对模型执行逻辑.另一方面,如果您想处理更多逻辑,您希望将该逻辑放在一个服务对象中,该服务对象处理创建 MyList 及其关联的 Wishes 或您的 MyList 模型.不过,我可能会选择服务对象.请注意,服务对象应该是一个类而不是一个模块.

Now, if all you want to create a MyList Object then you could just do the logic directly to the model. If, on the other hand, you want to handle some more logic you'd want to put that logic in a Service Object that handles the creation of a MyList and its associated Wishes or your MyList model. I would probably go for the Service Object though. Do note, the service object should be a class and not a module.

这篇关于Rails 路由和控制器模块 - 命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆