0

In the app I'm working on, Courses have many Problems, which in turn have many Steps. Right now there is a form for adding Problems to Courses (and then Steps can be added to those problems). What we want is to have a form that just has a field for LaTeX input, and then process the TeX to create multiple problems with their steps.

At the moment, we're doing this all in the Problems controller. We have two methods, texnew which is identical to new except it has a different view that redirects to the other new method: texcreate, which uses helper methods to extract the problems and steps (using a series of regexes), tries to create them, and flashes somewhat informative error messages if something goes wrong.

The thing is, I keep on reading that we're really not supposed to be doing a bunch of stuff in the controller, and we should favor doing things in the model instead. Virtual attributes might be the right idea for taking in a text field and processing it to create a single problem, but I can't figure out how to make it work for multiple problems, or how to generate any sort of error messages if something goes wrong somewhere along the way.

Is there some better/more idiomatic way to do this?

sapphiremirage
  • 475
  • 2
  • 17

1 Answers1

1

You don't really need virtual attributes for this if all your relationships are setup properly. You can use the new rails3 nested attributes. There is a good article on them here. This will allow you to rely more on model validation logic and keep the lean controller fat model idiom that rails encourages.

Simon Elliston Ball
  • 4,375
  • 1
  • 21
  • 18
  • I'm not sure how I would go about using these nested attribute forms to my advantage. The example has fields for child models and gets everything it needs to define the children through those fields, whereas I need to use just one field to create all of my objects. I still have a lot of processing to do once my form is submitted, and I don't know where I'm supposed to be doing that. – sapphiremirage Jun 13 '11 at 18:32
  • Right now, I'm thinking of at least refactoring to a "TeX Controller" which will have a new and create somewhat similar to the current texnew and texcreate in the problems controller (and allow me to remove those non-RESTful methods). However, this solution still does all of the work in the controller... – sapphiremirage Jun 13 '11 at 18:34