Leahcim
Leahcim

Reputation: 41919

Rails - Method in Model or Controller or somewhere else?

I'm practicing my Rails development skills by building an app that will have different types of exercises for users. Most probably things like multiple choice questions for different subject matters.

One way to check to see if the questions are answered correctly is to use validations on the model. However, I don't really need to save the results, and it might end up in me creating a lot of different models, as each question will have its own validation to check each answer.

Is an alternative to create a new Controller action for each subject area? Is

How else might I organize this?

Upvotes: 0

Views: 103

Answers (2)

Markus Buschhoff
Markus Buschhoff

Reputation: 176

The debate on where to put logic is as old as the involved patterns themselves. For MVC, I decided to ask myself:

  • Is the logic involved essential for the model?
  • Would the model (in itself) function well without the logic?
  • Is the model free of any requirements towards the controller?
  • If I'd re-use the model, would I want to take the involved business logic along or would it be in the way?

My general advice: Put business logic as "low" (bottom: db, then model, then controller, then view) as possible without violating any of the following constraints:

  • Authentication and authorization does not belong to view. It's either a property of the controller (handle things like sessions and general api access rules, etc) or of the model (authorization: who may access which content?)
  • UI/display/input-method related things do not get into model or db. Don't let your model/db decide if and how to render html, xml or json.
  • Data consistency and integrity not in controller/view. Ideally, your data model only accepts valid data, uses transaction safety and reports back failure or success to the controller. Ideally, consistency is handled at db level.
  • A model shall be reusable with other controller/views in new project (think of switching to another web api). Too heavy constraints may render it unusable in new situations.
  • there might be more...

And generally: If in doubt, put in into the controller. ;)

Upvotes: 0

varatis
varatis

Reputation: 14740

http://www.enode.com/x/markup/tutorial/mvc.html

With the MVC pattern, Controllers are usually in control of manipulating data kept by Models.

It's good form to keep most of your logic in your Controller. I'm not sure what you mean about a new Controller action, but what you'll probably want to do is set up some sort of form in your View, (see form_for) and fire that off to the Controller. The Controller does validations or whatever you need it to do.

This should be helpful: http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html

So for example, you might have a form in your View (a quiz for example), calculate a user's score in the Controller, and save that as a field for a user in your database.

Upvotes: 0

Related Questions