A Rails Blog In VS Code-Create Posts

How To Create A Blog in VS Code — Part III — RailsSeries#Episode 05

J3
Jungletronics
8 min readJul 13, 2023

--

Let’s Create some Posts.

In this post:

You will:

Learn how scaffold works;

Learn how to use console;

Learn about db:seed 👌️
Fig. 0: Upload the last episode and you are good to go…

Let’s Get Started!

0#step — Download the last post here and prepare your vscode environment.

1#step — Create a new feature branch:

git checkout -b add_blog_posts

2#step — Create a Post object by scaffolding:

rails g scaffold post title:string body:text

invoke active_record
create db/migrate/20230712221836_create_posts.rb
create app/models/post.rb
invoke test_unit
create test/models/post_test.rb
create test/fixtures/posts.yml
invoke resource_route
route resources :posts
invoke scaffold_controller
create app/controllers/posts_controller.rb
invoke erb
create app/views/posts
create app/views/posts/index.html.erb
create app/views/posts/edit.html.erb
create app/views/posts/show.html.erb
create app/views/posts/new.html.erb
create app/views/posts/_form.html.erb
create app/views/posts/_post.html.erb
invoke resource_route
invoke test_unit
create test/controllers/posts_controller_test.rb
create test/system/posts_test.rb
invoke helper
create app/helpers/posts_helper.rb
invoke test_unit
invoke jbuilder
create app/views/posts/index.json.jbuilder
create app/views/posts/show.json.jbuilder
create app/views/posts/_post.json.jbuilder

By using scaffolding, you can quickly generate a basic CRUD interface for a model in Rails, saving you time and effort in setting up the initial structure of your application.

Rails will generate several files and directories based on the scaffold:

  1. Migration:
  2. Model: A model file (post.rb)
  3. Controller: A controller file (posts_controller.rb)
  4. Views (Index, New, Show, Create, Update, Edit, and Delete):

Additionally, the scaffold generator will also add corresponding routes for the Post resource in the config/routes.rb file, allowing you to access the generated CRUD actions through URLs.

3#step — Now Migrate:

rails db:migrate
== 20230712221836 CreatePosts: migrating ======================================
-- create_table(:posts)
-> 0.0115s
== 20230712221836 CreatePosts: migrated (0.0116s) =============================

4#step — GoTo Ubuntu store and get dbeaver-ce:

Fig. 1: DBeaver Community Free Universal Database Tool.

5#step — Create a SQLite DB connection, Download drivers and view table (F4) and…

Fig. 2: By choosing dblite3 the tool will automatically download the drivers…That’s awesome!
Fig. 3: …access your dblite3 file inside db.

6#step — Let’s understand how to use rails console:

rails console
post_1 = Post.new(title:'Blog 1', body:'First post content!')
post_1.save
post_2 = Post.new(title:'Blog 2', body:'Second post content!')
post_2.save
Post.all
Post.create(title:'Blog 3', body:'Third post content!')
Post.all
@post = Post.find(1)
@post.title
@post = Post.last
@post.title
@posts = Post.last(2)
@posts
exit()
irb(main):002:0> post_1 = Post.new(title:'Blog 1', body:'First post content!')
irb(main):003:0> post_1.save
TRANSACTION (0.1ms) begin transaction
Post Create (0.3ms) INSERT INTO "posts" ("title", "body", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["title", "Blog 1"], ["body", "First post content!"], ["created_at", "2023-07-12 23:00:56.458161"], ["updated_at", "2023-07-12 23:00:56.458161"]]
TRANSACTION (8.5ms) commit transaction
=> true
irb(main):004:0> post_2 = Post.new(title:'Blog 2', body:'Second post content!')
irb(main):005:0> post_2.save
TRANSACTION (0.0ms) begin transaction
Post Create (0.3ms) INSERT INTO "posts" ("title", "body", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["title", "Blog 2"], ["body", "Second post content!"], ["created_at", "2023-07-12 23:01:14.428811"], ["updated_at", "2023-07-12 23:01:14.428811"]]
Post Load (0.2ms) SELECT "posts".* FROM "posts"
=>
[#<Post:0x00007f3b99d29390
id: 1,
title: "Blog 1",
body: "First post content!",
created_at: Wed, 12 Jul 2023 23:00:56.458161000 UTC +00:00,
updated_at: Wed, 12 Jul 2023 23:00:56.458161000 UTC +00:00>,
#<Post:0x00007f3b99d28fd0
id: 2,
title: "Blog 2",
body: "Second post content!",
created_at: Wed, 12 Jul 2023 23:01:14.428811000 UTC +00:00,
updated_at: Wed, 12 Jul 2023 23:01:14.428811000 UTC +00:00>]
irb(main):007:0> Post.create(title:'Blog 3', body:'Third post content!')
TRANSACTION (0.0ms) begin transaction
Post Create (0.3ms) INSERT INTO "posts" ("title", "body", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["title", "Blog 3"], ["body", "Third post content!"], ["created_at", "2023-07-12 23:01:52.211931"], ["updated_at", "2023-07-12 23:01:52.211931"]]
=>
#<Post:0x00007f3b9aacf3c8
id: 3,
title: "Blog 3",
title: "Blog 1",
created_at: Wed, 12 Jul 2023 23:00:56.458161000 UTC +00:00,
updated_at: Wed, 12 Jul 2023 23:00:56.458161000 UTC +00:00>,
#<Post:0x00007f3b9b2408c8
id: 2,
title: "Blog 2",
body: "Second post content!",
created_at: Wed, 12 Jul 2023 23:01:14.428811000 UTC +00:00,
updated_at: Wed, 12 Jul 2023 23:01:14.428811000 UTC +00:00>,
#<Post:0x00007f3b9b240788
id: 3,
title: "Blog 3",
body: "Third post content!",
created_at: Wed, 12 Jul 2023 23:01:52.211931000 UTC +00:00,
updated_at: Wed, 12 Jul 2023 23:01:52.211931000 UTC +00:00>]
irb(main):009:0> @post = Post.find(1)
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
=>
#<Post:0x00007f3b9c437a90
...
irb(main):010:0> @post.title
=> "Blog 1"
irb(main):011:0> @post = Post.last
Post Load (0.3ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT ? [["LIMIT", 1]]
=>
#<Post:0x00007f3b99ceab40
...
irb(main):012:0> @post.title
=> "Blog 3"
Fig. 4: Three posts created in rails console!

7#step — Test your app:

rails s

Fig. 5: Point to: http://127.0.0.1:3000/posts

8#step — Stop server (ctrl+c) and run these commands:

rails db:drop
rails db:migrate

9#step — GoTo seed.rband type:

rails-blog-demo/db/seeds.rb

10.times do |i|
Post.create(title: "Title #{i}", body: "Body #{i} words goes here idk...")
end

It will create 10 posts and all you have to do is…

10#step — Run seed in Terminal:

rails db:seed
Fig. 6 : Rails seeds are useful since they help us populate the test and development DBs with data.

11#step — Run these commands and Follow the sequence of step#7 of the previous post, which are:

git status
git add -A
git commit -m ":lipstick: feat: Add Blog Posts"
git push - set-upstream origin add_blog_posts

And:

#1. Press "Compare & pull request" button;
#2. Type the motivation of this request;
#3. Press "Create pull request" button;
#4. Review changes (comment, approve or request changes);
#5. Press "Submit review" button;
#6. Press "Merge pull requst" button;
#7. Press "Confirm merge" button;
#7. Press "Delete branch" And there you go! 👌️

These steps outline the process of contributing changes to a repository on GitHub:

  1. Press Compare & pull request button: This initiates the process of creating a pull request (PR) to merge changes from one branch into another.
  2. Type the motivation of this request: In this step, the contributor typically writes a description explaining the purpose and motivation behind the proposed changes. This helps the maintainers understand the intent of the PR.
  3. Press Create pull request button: After providing the necessary information, the contributor creates the pull request, formally submitting the proposed changes for review.
  4. Review changes (comment, approve or request changes): The pull request is then reviewed by other contributors or maintainers of the repository. They may comment on the changes, approve them if they are satisfied, or request further modifications if necessary.
  5. Press Submit review button: Once the review process is complete, the reviewer submits their review, indicating whether they approve the changes, request modifications, or have additional comments.
  6. Press Merge pull request button: If the changes are approved and ready to be merged, the repository maintainer or someone with merge permissions presses the “Merge pull request” button. This merges the changes into the target branch.
  7. Press Confirm merge button: After pressing the “Merge pull request” button, a confirmation step usually follows to ensure that the merge action is intentional.
  8. Press Delete branch: Finally, the branch containing the changes is typically deleted after it has been successfully merged. This helps keep the repository clean and organized.

By following these steps, contributors can effectively propose, review, and merge changes into a GitHub repository, maintaining a collaborative and organized development workflow.

And now type:

git status
git add -A
git commit -m ":lipstick: feat: Add Blog Posts"
git push --set-upstream origin add_blog_posts

12#step — Upload to Heroku:


git push heroku master
heroku run rails db:migrate
heroku run rails db:seed
heroku open
Fig. 7: https://j3-rails-blog-demo-5a0a55d44e12.herokuapp.com/posts/

For your convenience:

git checkout -b add_blog_posts
rails g scaffold post title:string body:text
rails db:migrate
clear
rails c
rails s
rails db:drop
rails db:migrate
rails db:seed
rails s
clear
git status
git add -A
git commit -m ":lipstick: feat: Add Blog Posts"
git push --set-upstream origin add_blog_posts
git status
git fetch
git status
git pull
git push heroku master
git branch -v
git checkout master
git pull
git push heroku master
heroku run rails db:migrate
heroku run rails db:seed
heroku open

That’s all folks!

In the next post, we will learn about some Post Tips & Tricks like custom migrations, metrics, and so on…

Bye for now!

👉️ rails_blog_v3

Related Posts:

00# Episode — RailsSeries — Installing Ruby on Rails Using ASDF — Why ASDF is Better Than RBENV for Rails Bootstrap App?

01# Episode — RailsSeries — How To Send Email In Rails 7? — User Registration and Onboarding.

02# Episode — RailsSeries — 14 Ruby Extensions 4 Vs Code — Based On This Deanin’s video.

03# Episode — RailsSeries — A Rails Blog In VS Code — Quick Start — How To Create A Blog in VS Code — Part I

04# Episode — RailsSeries — A Rails Blog In VS Code — Styling — How To Create A Blog in VS Code — Part II

05# Episode — RailsSeries — A Rails Blog In VS Code — Create Posts — How To Create A Blog in VS Code — Part III (this one)

06# Episode — RailsSeries — A Rails Blog In VS Code — Posts Tips&Tricks — How To Create A Blog in VS Code — Part IV

07# Episode — RailsSeries — A Rails Blog In VS Code — Devise — How To Create A Blog in VS Code — Part V

08# Episode — RailsSeries — A Rails Blog In VS Code — Add Comments to Post — How To Create A Blog in VS Code — Part VI

09# Episode — RailsSeries — Rails Blog In VS Code — Using Stimulus — How To Create A Blog in VS CodePart VII

10# Episode — RailsSeries — Rails Blog In VS Code — Noticed V1 — Notifications for your Ruby on Rails app — Part VIII

11# Episode — RailsSeries — Rails Blog In VS Code — Noticed V2 — Notifications for your Ruby on Rails app — Part IX

For v3 let’s Tag it all!

git tag -a rails_blog_v3 -m "Blog in Rails 7 - v1.0:  Go to  https://j3-rails-blog-demo-5a0a55d44e12.herokuapp.com/" -m "0- Creating a new feature branch: add_blog_posts;" -m "1- Scaffolding to create Posts;" -m "2- Use Bootstrap 5 CDN & Navbar;" -m "3- Modify Home & About pages;" -m "4- Making adjustments on layout app - yield wrap around ;" -m "5- Upload Rails 7 project to heroku." -m "Thank you for downloading this project 😍️" 

git push origin rails_blog_v3

Deleting Remote and cleanning Local Tags:

git tag -a rails_blog_v3 -m "Blog in Rails 7 - v1.0: Go to https://j3-rails-blog-demo-5a0a55d44e12.herokuapp.com/" -m "0- Creating a new feature branch: add_blog_posts;" -m "1- Scaffolding to create Posts;" -m "2- Use Bootstrap 5 CDN & Navbar;" -m "3- Modify Home & About pages;" -m "4- Making adjustments on layout app - yield wrap around ;" -m "5- Upload Rails 7 project to heroku." -m "Thank you for downloading this project 😍️"

git push origin rails_blog_v3

--

--

J3
Jungletronics

😎 Gilberto Oliveira Jr | 🖥️ Computer Engineer | 🐍 Python | 🧩 C | 💎 Rails | 🤖 AI & IoT | ✍️