Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-4scaffolding

Scaffold is a good or normal pratice in Rails?


I'm a PHP developer, and any framework I've used, scaffold is recommended just for tests.

So, my question is: in Rails too?

It's recommend use scaffold in production (for just a CRUD, example: a blog)

Because, on thing is different: in some PHP frameworks, the scaffold views is processed/created in each requisition, but in Rails (I think), the files are already created.


Solution

  • You need to be aware that if you generate a Rails scaffold for a model, it'll generate all the code required to create, read, update and delete (CRUD) stuff in your database. You probably don't want that kind of priviledge given over to all users in a production environment (particularly update and delete) so that's why it's dangerous to blindly upload scaffold code to production.

    If you do create a scaffold, you should go through it and remove all the parts you don't want to be exposed in a production environment. For a simple blog, you probably only want the index and show functions and remove the ability to create, update and destroy entries (or at least protect them with some authentication solution so that only you can do that).

    I would say that the code generated by a scaffold is fine to use in production, it's more that you need to be careful what priviledges you give to public users.

    If nothing else, scaffolds are very good for learning about how database driven applications work.

    Depending on the situation, I do scaffold my models when I generate them and then remove stuff I don't need and tailor it to my requirements (including adding tests). I usually find that quicker than writing all the code from scratch (especially when it generates the database migration file, test files and adds the model resource to the routes file at the same time).