Clean your Rails routes: grouping

栏目: IT技术 · 发布时间: 4年前

内容简介:InYou add more and more routes in this file as your project grows.The problem here is that this file potentially become very complicated to manage over the time.

How to separate & group your routes by logical entities

Feb 20 ·2min read

In Ruby on Rails , all the routes of a given application can be found within the config/routes.rb file.

You add more and more routes in this file as your project grows.

The problem here is that this file potentially become very complicated to manage over the time.

That’s why it’s important to find a way to order and maintain your routes.

Let’s have a look to an example to see how we could achieve this task.

The my-blog application

Few month ago, we created our my-blog application. This application allows you creating and manipulating blogposts.

Also, it provides an admin dashboard.

So the config/routes.rb looks like as following

Here we can clearly isolate 3 entities:

users
posts
admin

So let’s see how to extract each of the entities in a separate file.

First, we have to manipulate our config/application.rb file

This line will redefine the target of our config.paths['config/routes.rb'] .

The target is now all the Ruby files under the config/routes/ directory.

So when your application receives a request, Rails will search the expected route in the routing table defined under the config/routes directory.

Ok. So now, let’s have a look to the files in this directory

$>  $ tree config/routes
config/routes
  ├── base.rb
  ├── admin.rb
  ├── posts.rb
  └── users.rb0 directories, 4 files

Note that each file uses the Rails.application.routes.draw method to declare each resources.

Finally, you can remove the config/routes.rb file.

Conclusion

With this implementation, As things progress, you’ll be able to maintain your routes in a very easy way.

And that will be a good introduction to your app for new developers.

Voilà!


以上所述就是小编给大家介绍的《Clean your Rails routes: grouping》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

马云现象的经济学分析:互联网经济的八个关键命题

马云现象的经济学分析:互联网经济的八个关键命题

胡晓鹏 / 上海社会科学院出版社 / 2016-11-1 / CNY 68.00

互联网经济的产生、发展与扩张,在冲击传统经济理论观点的同时,也彰显了自身理论体系的独特内核,并与那种立足于工业经济时代的经典理论发生显著分野。今天看来,“马云”们的成功是中国经济长期“重制造、轻服务,重产能、轻消费,重国有、轻民营”发展逻辑的结果。但互联网经济的发展却不应仅仅止步于商业技巧的翻新,还需要在理论上进行一番审慎的思考。对此,我们不禁要问:互联网经济驱动交易发生的机理是什么?用户基数和诚......一起来看看 《马云现象的经济学分析:互联网经济的八个关键命题》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具