Why do Ruby on Rails professionals NOT use Scaffolding?

后端 未结 8 1204
梦毁少年i
梦毁少年i 2020-12-04 09:59

I read sometimes from people that seem to be working with rails since longer, that one important lesson they learnt would be \"Don\'t use scaffolding\". Also on irc I read c

相关标签:
8条回答
  • 2020-12-04 10:07

    I believe most professionals avoid scaffolding because they prefer a test driven development approach to writing production code. This means they want to write a failing test first and then write the code that makes the test pass. This is a great way of producing strong code, but it works best at a very granular level. Scaffolding seems to do too much at one time and so it disrupts a tight loop of writing a failing test for a specific features then writing the code that makes that particular feature pass. It may be more important to get into that habit above and beyond the ease of use of scaffolding.

    That said scaffolding can be pretty powerful in its own right.

    0 讨论(0)
  • 2020-12-04 10:10

    I'm experienced with rails and I rarely use scaffolding simply because my end goal is far from simple CRUD actions. However, there's no strict rule to not use scaffolding. Some coders frown on it because it is actually scaffolding, literally. It's not a finished product. Scaffolding supports you as you build the actual product. Search google images for "scaffolding" and you should get the idea.

    Keep in mind scaffold is just one of many built-in generators in Rails. A Rails generator is simply a script that outputs some generic code. Generators are very useful time-savers, and you'll quickly find yourself writing generators for your own custom needs.

    0 讨论(0)
  • 2020-12-04 10:10

    It is important to understand the use of rails generate scaffold and be aware of its limitations. Scaffolding helps you to get something running quickly and test an assumption. But in the real world it will not get you too far. Lets say you created a model with scaffolding

    rails generate scaffold Article title:string body:text
    

    Great! You now have a prototype ready. But now lets say you have to add another field "author".

    rails generate migration add_to_article_author author:string
    rake db:migrate
    

    now the table articles has a new column but the files in /app/views/articles are in the same old state i.e. the form will not have author field etc. If you run scaffold again

    rails generate scaffold Article title:string author:string body:text --skip-migration
    

    This time you have added --skip-migrate because that has been done before and Rails will really complain if you were to migrate the same table again. Now scaffold will prompt you to overwrite the file it created the first time. Overwriting will also destroy any changes you made to your controller /app/controllers/article_controller.rb or views files like /app/views/article/show.html.erb or index.html.erb

    Since any worthwhile Rails app has custom code (and not boilerplate code created by scaffold) a Rails programmer should use scaffold only to test an idea. Use scaffold to give your client something to play with. But in real world boilerplate scaffold code is not used.

    0 讨论(0)
  • 2020-12-04 10:14

    People thus far have said that experienced rails programmers don't use scaffolding, and for good reasons mainly. I advocate that beginners don't use scaffolding either.

    You may have been plodding along happily for some time with scaffolding, and then you realise that you forgot to include the published boolean field for your Post model, so you generate a migration to add the attribute to the table (you've managed to work that out). You then also have to work out how to add that to the form that scaffold produced. Then for the life of you you can't work out why it's not updating when you submit your form, and after much strife and wasted time you figure out you haven't permitted mass assignment on that attribute like scaffold did for you on the others. Turns out you don't really know anything about how Rails works.

    If you're learning Rails, you'd understand much more about MVC if you built the M the V and C yourself.

    0 讨论(0)
  • 2020-12-04 10:15

    Scaffolding isn't really meant for production use. It's meant to get an application bootstrapped quickly and then it can be modified or done away with.

    The Rails 3 scaffolding is actually pretty decent, but it still lacks some things like a way to handle nested resources and it doesn't use the simpler respond_with (over respond_to, which encourages verbosity where it isn't needed or welcome).

    It's unlikely that the default scaffold forms will work un-modified, either — you probably have relationships between your models that translate to a column in the database like user_id. When creating a scaffold of a model with a relationship, this column shows up as a text field in the form when clearly it should be inferred from the URL or selected via another, more user-friendly, interface.

    There are a lot of small details like this that make scaffolding a really unlikely candidate for production-ready-out-of-the-box code. You can certainly build an application by generating the scaffold and then filling in the gaps and cleaning up areas you don't need, though, and I suspect most Rails developers do this to some extent.

    0 讨论(0)
  • 2020-12-04 10:21

    I think scaffolding is very good to check out the new stuff of new rails versions (for example now in rails 4 the params permit stuff in a controller). You can use it for fast prototyping. Often it is simple not necessary to generate a full scaffold..

    0 讨论(0)
提交回复
热议问题