Scaffold: A quickest way of building a blog with posts and comments 2020
bogotobogo.com site search:
Note
In the subsequent chapters we will create more realistic blog. However, before we do that as a warm up, here we'll create a very simple blog which a user can add posts and comments. So, this chapter will be an express course for making a blog.
Initial steps to take
Rails scaffolding is a quick way to generate some of the major pieces of an application. If we want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.
- The first thing we need to do is to create the Rails
application:
$ rails new blog
- In the blog app directory, use the scaffold generator to create the MVC components needed for posts and comments:
$ cd blog $ rails generate scaffold post title:string body:text $ rails generate scaffold comment post_id:integer body:text
Here, we ran a couple of code generators to create The posts as well as the comments. - Create the post and comment database tables:
$ rake db:migrate
Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations - Start the web server:
$ rails server
and then point our browser to: http://localhost:3000/posts
If we click 'Create":
We can do comment as well:
At "Create" button press:
$ rake routes Prefix Verb URI Pattern Controller#Action comments GET /comments(.:format) comments#index POST /comments(.:format) comments#create new_comment GET /comments/new(.:format) comments#new edit_comment GET /comments/:id/edit(.:format) comments#edit comment GET /comments/:id(.:format) comments#show PATCH /comments/:id(.:format) comments#update PUT /comments/:id(.:format) comments#update DELETE /comments/:id(.:format) comments#destroy posts GET /posts(.:format) posts#index POST /posts(.:format) posts#create new_post GET /posts/new(.:format) posts#new edit_post GET /posts/:id/edit(.:format) posts#edit post GET /posts/:id(.:format) posts#show PATCH /posts/:id(.:format) posts#update PUT /posts/:id(.:format) posts#update DELETE /posts/:id(.:format) posts#destroyThis "rake routes" lists all the URLs currently recognized by our app.
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization