LuckyLuke
LuckyLuke

Reputation: 49107

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.

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?

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?

Upvotes: 2

Views: 131

Answers (2)

David Morales
David Morales

Reputation: 18064

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.

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.

Upvotes: 1

user1191764
user1191764

Reputation: 1

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!

Upvotes: 0

Related Questions