何时在 rails 中创建新控制器 [英] When to create a new controller in rails

查看:51
本文介绍了何时在 rails 中创建新控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道您何时知道需要在 Rails 应用程序中创建控制器.

I'm wondering when you know that you need to create a controller in a rails application.

例如,我正在学习使用 Rails 进行敏捷 Web 开发中的教程,该应用程序创建了多个模型,所有模型都具有单独的视图和控制器.然而,我们随后也创建了一个 Store 控制器,但没有与之关联的模型.为什么我们需要一个没有模型的控制器?模型的控制器不能处理所有必需的操作吗?

For example, I'm going through the tutorial in Agile Web Development with Rails and the application creates several models, all with seperate views and contollers. However, we also then create a Store controller, but no model associated with it. Why do we need a controller with no model? Couldn't the controllers for the models handle all required operations?

这很常见吗?如果是这样,您如何确定什么时候需要控制者?

Is this common? If so, how do you determine when something warrants a controller?

谢谢!

这些答案有帮助,谢谢.

These answers help, thank you.

我担心的是,当我自己开发某些东西时,我会开始创建无用的控制器,或者反过来说,不会创建必要的控制器.但是,我想我需要停止将控制器和模型视为 1-1 关系,对吗?如果我理解正确,可能有多个控制器访问一个模型,而一个控制器中可能使用多个模型?

My concern is that when I develop something on my own, I will begin to create useless controllers, or on the flip side, not create necessary controllers. But, I suppose I need to stop thinking of controllers and models as a 1-1 relationship, correct? If I'm understanding correctly, there could be many controllers accessing a model and many models being used in one controller?

推荐答案

更新:我强烈推荐阅读 DHH 如何组织他的 Rails 控制器这几乎比我原来的答案解释得更好.

UPDATE: I highly recommend reading How DHH Organizes His Rails Controllers which pretty much explains it much better than my original answer.

我认为如果你换一种方式,这个问题会更合适:

I think the question would be more suitable if you'd put it another way:

为什么每个控制器都需要模型(在这种情况下为 AR)?

答案当然是,你没有.当您考虑控制器时,最好不要考虑数据,而是稍微退后一步,考虑资源.如果你在互联网上搜索 REST,你会发现很多文章,其中大部分都会包含对术语resourcerepresentation 的各种解释.简而言之,让我们过分简化并说资源是值得一提的一切.文章是一种(集合)资源.商店是一个(单一的,成员)资源.

And the answer of course is, you don't. When you are thinking about controllers it's best not to think about data, but take a slight step back, and think about resources. If you search for REST in internet, you will find a lot of articles and most of them will include various explanations of terms resource and representation. To make this story short, let's just oversimplify and say that resource is everything that's worth mentioning. Articles is a (collection) resource. Store is a (singular, member) resource.

以登录用户为例.您可能已经拥有 UsersController,它(默认情况下)将允许您添加新用户(创建资源)、删除它们(删除资源)、显示单个用户以及所有用户.如果您只考虑数据和控制器,您可能会开始在 UserController 中创建其他操作,例如 login_user,这是一种气味.如果您考虑资源,那就是所有值得一提的内容或为其创建 URI",您可能会认为您需要另一个资源,那就是:sessions.这样想:当用户登录时,他实际上创建了一个会话资源.退出后,您可以删除、移除资源.我推荐的 Rails 教程 书中对此进行了更好的解释:http://ruby.railstutorial.org/chapters/sign-in-sign-out#sec:sessions

Take signing in users for example. You probably already have UsersController which (by default) will allow you adding new users (create resource), deleting them (remove resource), displaying single user and also all users. If you just think in terms of data and controllers, you probably would start creating additional actions like login_user in UserController, which is a smell. If you think about resources, and that is "everything that's worth mentioning about or creating URI for it", you might think that you need another resource, and that is: sessions. Think about this way: when user signs in, he actually creates a session resource. And with sign out, you delete, remove the resource. It is much better explained in the Rails tutorial book which I recommend: http://ruby.railstutorial.org/chapters/sign-in-sign-out#sec:sessions

总结一下,这可能会帮助您确定何时需要新控制器:

To recap, this may help you in figuring out when you need new controller:

  • 当您考虑将非 RESTful 操作(例如 log_incalculate_date 等)放入控制器时.
  • 当有一些东西可以命名并且有趣"到足以成为一个单独的资源时.
  • 此外,当您以由外而内"的方式进行开发时,这样的答案会更自然:http://rubylearning.com/blog/2010/10/05/outside-in-development/
  • When you think about putting non RESTful actions in controller like log_in, calculate_date, ect.
  • When there is something that you can name and that is "interesting" enough to be a separate resource.
  • Also, when you are developing in "outside in" style, such answers come more naturally: http://rubylearning.com/blog/2010/10/05/outside-in-development/

总的来说,了解 REST 及其哲学会大有帮助.

Overall, learning about REST and its philosophy will help a lot.

这篇关于何时在 rails 中创建新控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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