views:

103

answers:

2

in my project i have about 20 html forms with many different <input>'s. This <input>'s are unique in every form and they don't repeated between them. For each form there is a script which get data from form, generate specific file, and push it to the browser. and that's it. no databases, admin, logins/passwords and other usual web-app stuff.

so for example in php, project structure can be something like this:

forms/

------->form1/

--------------->index.html

--------------->script/

----------------------->index.php

------->form2/

--------------->index.html

--------------->script/

----------------------->index.php

and so on. It's quite clear and it makes pretty urls like:

www.website.com/forms/form1

but in Ruby-on-Rails there is a MVC pattern. And i have no idea how to organize structure with project like that. How to make it right? I should not to make 20 different controllers after all, right?

A: 

Yes you do make 20 controllers. In much the same way as you have 20 script/index.php files in your PHP structure. The rails structure would look something like this.

app/
    controllers/
        form1s_controller
        form2s_controller
        .....
        formns_controller
    view/
        form1/
            new.html.erb
        form2/
            new.html.erb
        ......
        formn/
            new.html.erb
        layouts/
            application.html.erb

Where the controllers have a method for each action that you want to perform e.g. new, edit etc

The layouts/application.html.erb file will contain all of the markup that is consistent across all of your pages.

Finally, don't try and fight the structure. It is there to enable minimal code because of convention, if you try and create your own structures you will create a world of pain.

Steve Weet
Thanks for answer. Well in every tutorial/screecast that i see, was just about 3 or 4 controllers. And i think that 20, 30 and more controllers it's just bad project organize. So, it's normal?
cru3l
Well most tutorials/screencasts are by their nature simplifying things. You would expect one controller for each "area" of your application whether that is a single form with no database backing or a set of forms that encapsulate a particular area of your application. Only you will know this but there are certainly many ROR apps out there with many more controllers than that. Redmine for example (A ROR issue tracker http://github.com/edavis10/redmine/tree/master/app/controllers/) has more than 40 controllers
Steve Weet
A: 

Since there is no database backend for the forms, i would create one FormsController, with 20 methods (form1, form2, form3, ...).

Your urls would then look almost the same \forms\form1, \forms\form2 ...

Even if you would use 20 controllers, there are many ways in ruby to remove duplicate code. So even in a normal MVC, i have had applications with 140 models, and likewise as many controllers, most of these controllers had only one line and the views were completely generic.

Just because a certain solution seemed simple in PHP, it doesn't mean better solutions don't exist. Like for instance MVC. It might seem to induce more code, more files, more work, but the structure is there for good reason, it seperates the concerns cleanly, and each file in itself becomes more clear and easier to understand.

nathanvda
if i will use one FormsController, how i implement a script for each form? these "scrips" are quite big, and looks that each "script"-method will be placed in same FormsController, right? Or there is a way to use separate file for each "script" like in php?
cru3l
In MVC, there is a model that collects the data that needs to be shown or edited. The controller retrieves the correct models, and hands them down to the view.The view can manipulate the models and present them in the correct way. It is the intention that the view is as simple as possible. If big or complicated manipulations are needed to present the data correctly, we use a library or presenter or helpers. From the view, at submit, the user-input data is then sent back to the controller to process.Could you describe in short what your "script" does? so i can think along.
nathanvda
The main goal it's get a special text file, based on values from form's input. I wrote a class for easier make this "special format text files", called FileMaker. So what's exactly doing every script? It starts with `$filemaker = new FileMaker();` , then getting all values from form, and in dependence of these values doing something in `$filemaker`. For example, `if ($_GET['somecheckbox'] == true ? $filemaker->addProduct("some line") : $filemaker->addBlock('some block')` . It's impossible to make one BIG script from 20 scripts. And that's why we need controller for every form, i think.
cru3l