Rails 控制器命名空间 [英] Rails Controller Namespace

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

问题描述

在 ruby​​ on rails 中使用命名空间的优缺点是什么.例如:我有很多像

What are advantages and disadvantages of using namespace in ruby on rails. For example: I've many controllers like

CompanyLocations 
CompanyXXXX 
CompanySports 
CompanyActivites
CompanyQQQQQ

我想将所有这些控制器放在公司文件夹中.这方面的 Rails 最佳实践是什么?

I want to put all these controllers in Company folder. What is the rails best practice for this ?

推荐答案

你必须在你的控制器/目录中创建一个子文件夹,在你的视图/目录中也是如此.

You have to create a subfolder inside your controller/ directory, and the same in your views/ directory.

你的控制器文件应该是这样的

Your controller file should look like

module Company
 class SportsController < ApplicationController

 def index
 end

 end
end

...或

class Company::SportsController < ApplicationController

 def index
 end

end

你也可以这样调用你的partials

You can also call your partials this way

render :template => "company/sports/index"

然后在 routes.rb

Then in routes.rb

namespace :company do
 resources :sports
end

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

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