简体   繁体   English

Ruby on Rails中的控制器类应包含什么?

[英]What should a controller class in Ruby on Rails contain?

I am making a quiz web application so there is a need to administrate questions (add, change etc) and the users will browse the collection and possible add, report etc questions. 我正在制作一个测验Web应用程序,因此需要管理问题(添加,更改等),并且用户将浏览集合并可能添加,报告等问题。

Now, if I made a questioncontroller would this contain all actions(methods) for dealing with questions (both public and admin) or is this a case where you make two controllers? 现在,如果我创建了一个questioncontroller,它将包含所有用于处理问题(公共和管理员)的操作(方法),还是您要创建两个控制器的情况?

Could someone explain when I should make new controllers instead of just adding actions to one existing? 有人可以解释我何时应该制作新的控制器,而不仅仅是在现有控制器上添加操作?

And if I made two controllers, I would end up duplicating a lot of code. 而且,如果我制造了两个控制器,最终将导致重复很多代码。 Should I make a base class? 我应该做基础课吗?

You could build everything using a single controller. 您可以使用单个控制器来构建所有内容。 I think using multiple controllers is something related to the project structure. 我认为使用多个控制器与项目结构有关。

In your case I would implement all kind of questions in the same controller (named "questions_controller.rb", for example), and inside I would implement a filter to authenticate admin users when requesting admin questions. 在您的情况下,我将在同一个控制器中(例如,名为“ questions_controller.rb”)实施所有类型的问题,而在内部,我将实施过滤器以在请求管理员问题时对管理员用户进行身份验证。

It's very probable that you will need other controllers for your app, such as static pages (contact, about), sessions (login, logout), members (register, profile), etc. 您的应用很可能需要其他控制器,例如静态页面(联系,关于),会话(登录,注销),成员(注册,个人资料)等。

About the base class, sure! 关于基类,当然! If you are repeating methods it would be a nice solution. 如果您要重复方法,那将是一个不错的解决方案。 Rails is just Ruby. Rails只是Ruby。

It's not neccessay to create two controller, if you considered administrator as normal users. 如果您将管理员视为普通用户,则不必创建两个控制器。 If you need a special user to administrate the questions as admin, Creating two controllers is needed! 如果需要特殊用户以管理员身份管理问题,则需要创建两个控制器!

That's my options. 那是我的选择。 Hope can help you! 希望可以帮到您!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM