Rails Scaffold

2018-02-08 10:20:01来源:oschina作者:Kolosek人点击

分享

Scaffolding in Ruby on Rails refers to theauto-generation of a set of a model, views and a controller usually used for a single database table.


For example, you can auto-generate a ready to use controller, model, and views with a full CRUD( C reate, Read, Update, Delete) web interface for the Story table using the following command:


$rails generate scaffold Story title:string content:text


It's way easier to do this, instead of coding everything yourself, it saves you a lot of time!


Scaffold or Models?

Compared to thescaffold that generates everything that you need (and do not need),modelscreate only some related components. The best, and my favorite, way to explain the difference between scaffold and models is by using the following example:


Generate models

Once you enter the command $rails generate model Story title:string content:textyou will generate:


invokeactive_record
createdb/migrate/20180205103025_create_stories.rb
createapp/models/story.rb
invoketest_unit
createtest/models/story_test.rb
createtest/fixtures/stories.yml

invoke active_record will tie your model to the database, while the next line createsa migration file. Migrations are used to alter your database schema. This migration file creates the database table called 'stories', and database columns for "title" and "content ".


The third line will create a model - a Ruby class that inherits the Active Record. With this, every method that can be called in Active Record can now be called in your model. The last three lines create relatedtest files for your model.



While scaffolding will get you up and running quickly, the code it generates is unlikely to be the perfect fit for your application. You'll most probably want to customize the generated code. Many experienced Rails developers avoid scaffolding entirely, preferring to write all or most of their source code from scratch. You can read more about this on RailsGuides .



As you can see, generate modelsdoes not create any kind of view to display information on a page. To have a complete, ready to use application, you would need to:

generate controllers (which in turn generates views, as well) or
generate scaffold (which generates your model, views, controller and writes to your routes.rb file).

If you change your mind and decide to use scaffold after already generating the model, you can always run$rails generate scaffold . It will create all the missing files.


Generate scaffold

If you enter the command $rails generate scaffold Story title:string content:textyou will generate the following files:


invokeactive_record
createdb/migrate/20180205103508_create_stories.rb
createapp/models/story.rb
invoketest_unit
createtest/models/story_test.rb
createtest/fixtures/stories.yml
invokeresource_route
routeresources :stories
invokescaffold_controller
createapp/controllers/stories_controller.rb
invokeerb
createapp/views/stories
createapp/views/stories/index.html.erb
createapp/views/stories/edit.html.erb
createapp/views/stories/show.html.erb
createapp/views/stories/new.html.erb
createapp/views/stories/_form.html.erb
invoketest_unit
createtest/controllers/stories_controller_test.rb
invokehelper
createapp/helpers/stories_helper.rb
invoketest_unit
invokejbuilder
createapp/views/stories/index.json.jbuilder
createapp/views/stories/show.json.jbuilder
createapp/views/stories/_story.json.jbuilder
invokeassets
invokecoffee
createapp/assets/javascripts/stories.coffee
invokescss
createapp/assets/stylesheets/stories.scss
invokescss
createapp/assets/stylesheets/scaffolds.scss

Once model related tests are created, the next line will generateresource routesto your stories. After generating resource routes comes thecontrollerand its actions (index, show, new, edit, create, update and destroy), together withviewsfor each of this actions.


The Rails router recognizes URLs and connecting them to a controller's action. By default, a controller's action will render a view of the same name.


Migration

Every time you create a migrationusing scripts (generate model / generate scaffold) a new migration is added to the correct directory. Youuse$rake db:migrateto checks which migrations that have not been added to the database.


== 20180205103508 CreateStories: migrating ====================================
-- create_table(:stories)
-> 0.0007s
== 20180205103508 CreateStories: migrated (0.0008s) ===========================Destroy

Everyone is free to edit and do the necessary changes to their application to work as intended, even if it means completelyremoving scaffold. You can remove scaffold in the following way:

Generate scaffold:$rails generate scaffold Story
If you migrated your files, perform a rollback:$rake db:rollback
Destroy or undo scaffold:$rails destroy scaffold Story

By doing this, you will delete all the files created by the scaffold but additional changes that you may have done manually will not be removed.


Conclusion

A scaffold is excellent to use when it comes tosimple examples, quick mockups or testing. However, be sure to generate your own models when you decide to develop a new application.


We hope you discovered something new today!


Originally posted on kolosek.com.


最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台