From 9c1c1ee7726005b444061c668d94847c48e6ba06 Mon Sep 17 00:00:00 2001
From: Ibrahim <98372964+abe2dev@users.noreply.github.com>
Date: Sat, 12 Mar 2022 14:13:13 -0800
Subject: [PATCH 01/12] Add the dependencies
---
Gemfile | 4 ++++
Gemfile.lock | 10 ++++++++++
2 files changed, 14 insertions(+)
diff --git a/Gemfile b/Gemfile
index aeead36..0680cc6 100644
--- a/Gemfile
+++ b/Gemfile
@@ -62,5 +62,9 @@ group :development do
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
+
+
+ gem 'faker'
+ gem 'pry-rails', '~> 0.3.9'
end
diff --git a/Gemfile.lock b/Gemfile.lock
index bf58548..a80008f 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -70,6 +70,7 @@ GEM
bootsnap (1.11.1)
msgpack (~> 1.2)
builder (3.2.4)
+ coderay (1.1.3)
concurrent-ruby (1.1.9)
crass (1.0.6)
debug (1.4.0)
@@ -77,6 +78,8 @@ GEM
reline (>= 0.2.7)
digest (3.1.0)
erubi (1.10.0)
+ faker (2.20.0)
+ i18n (>= 1.8.11, < 2)
globalid (1.0.0)
activesupport (>= 5.0)
i18n (1.10.0)
@@ -120,6 +123,11 @@ GEM
nokogiri (1.13.3-x86_64-linux)
racc (~> 1.4)
pg (1.3.4)
+ pry (0.14.1)
+ coderay (~> 1.1)
+ method_source (~> 1.0)
+ pry-rails (0.3.9)
+ pry (>= 0.10.4)
puma (5.6.2)
nio4r (~> 2.0)
racc (1.6.0)
@@ -188,9 +196,11 @@ PLATFORMS
DEPENDENCIES
bootsnap
debug
+ faker
importmap-rails
jbuilder
pg (~> 1.1)
+ pry-rails (~> 0.3.9)
puma (~> 5.0)
rails (~> 7.0.2, >= 7.0.2.3)
sprockets-rails
From 9c4fdb46d94d62b77591e3aaf3acb22d5108a107 Mon Sep 17 00:00:00 2001
From: Ibrahim <98372964+abe2dev@users.noreply.github.com>
Date: Sat, 12 Mar 2022 14:14:06 -0800
Subject: [PATCH 02/12] Create Posts model
---
app/models/post.rb | 4 ++++
db/migrate/20220312205739_create_posts.rb | 10 ++++++++++
db/schema.rb | 24 +++++++++++++++++++++++
3 files changed, 38 insertions(+)
create mode 100644 app/models/post.rb
create mode 100644 db/migrate/20220312205739_create_posts.rb
create mode 100644 db/schema.rb
diff --git a/app/models/post.rb b/app/models/post.rb
new file mode 100644
index 0000000..9096ad6
--- /dev/null
+++ b/app/models/post.rb
@@ -0,0 +1,4 @@
+class Post < ApplicationRecord
+ validates :title, presence: true, uniqueness: true
+ validates :body, presence: true, length: {minimum:50}
+end
\ No newline at end of file
diff --git a/db/migrate/20220312205739_create_posts.rb b/db/migrate/20220312205739_create_posts.rb
new file mode 100644
index 0000000..33b1de5
--- /dev/null
+++ b/db/migrate/20220312205739_create_posts.rb
@@ -0,0 +1,10 @@
+class CreatePosts < ActiveRecord::Migration[7.0]
+ def change
+ create_table :posts do |t|
+ t.string :title
+ t.text :body
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
new file mode 100644
index 0000000..dc73aae
--- /dev/null
+++ b/db/schema.rb
@@ -0,0 +1,24 @@
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# This file is the source Rails uses to define your schema when running `bin/rails
+# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
+# be faster and is potentially less error prone than running all of your
+# migrations from scratch. Old migrations may fail to apply correctly if those
+# migrations use external dependencies or application code.
+#
+# It's strongly recommended that you check this file into your version control system.
+
+ActiveRecord::Schema[7.0].define(version: 2022_03_12_205739) do
+ # These are extensions that must be enabled in order to support this database
+ enable_extension "plpgsql"
+
+ create_table "posts", force: :cascade do |t|
+ t.string "title"
+ t.text "body"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ end
+
+end
From 6e065d6dd64d9dce8df35ba78842cf7ddf4c7b8a Mon Sep 17 00:00:00 2001
From: Ibrahim <98372964+abe2dev@users.noreply.github.com>
Date: Sun, 13 Mar 2022 22:55:56 -0700
Subject: [PATCH 03/12] Create Posts controller, views and all RESTful routes
---
app/controllers/posts_controller.rb | 49 +++++++++++++++++++++++++++++
app/helpers/posts_helper.rb | 2 ++
app/views/posts/edit.html.erb | 29 +++++++++++++++++
app/views/posts/index.html.erb | 21 +++++++++++++
app/views/posts/new.html.erb | 29 +++++++++++++++++
app/views/posts/show.html.erb | 25 +++++++++++++++
config/routes.rb | 2 ++
7 files changed, 157 insertions(+)
create mode 100644 app/controllers/posts_controller.rb
create mode 100644 app/helpers/posts_helper.rb
create mode 100644 app/views/posts/edit.html.erb
create mode 100644 app/views/posts/index.html.erb
create mode 100644 app/views/posts/new.html.erb
create mode 100644 app/views/posts/show.html.erb
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
new file mode 100644
index 0000000..efb6808
--- /dev/null
+++ b/app/controllers/posts_controller.rb
@@ -0,0 +1,49 @@
+class PostsController < ApplicationController
+ def index
+ @posts = Post.all
+ end
+
+ def show
+ @post = Post.find params[:id]
+ rescue => e
+ redirect_to posts_path, alert: e.message
+ end
+
+ def destroy
+ @post = Post.find params[:id]
+ @post.destroy
+ redirect_to posts_path, { notice: "Post deleted successfully", status: 303 }
+ rescue => e
+ redirect_to posts_path, alert: e.message
+ end
+
+ def new
+ @post = Post.new
+ end
+
+ def create
+ @post = Post.new params.require(:post).permit(:title, :body)
+ if @post.save
+ redirect_to post_path(@post)
+ else
+ render :new, status: 303
+ end
+ end
+
+ def edit
+ @post = Post.find params[:id]
+ rescue => e
+ redirect_to posts_path, alert: e.message
+ end
+
+ def update
+ @post = Post.find params[:id]
+ if @post.update params.require(:post).permit(:title, :body)
+ redirect_to post_path(@post)
+ else
+ render :edit, status:303
+ end
+ rescue => e
+ redirect_to posts_path, alert: e.message
+ end
+end
diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb
new file mode 100644
index 0000000..a7b8cec
--- /dev/null
+++ b/app/helpers/posts_helper.rb
@@ -0,0 +1,2 @@
+module PostsHelper
+end
diff --git a/app/views/posts/edit.html.erb b/app/views/posts/edit.html.erb
new file mode 100644
index 0000000..4046e9d
--- /dev/null
+++ b/app/views/posts/edit.html.erb
@@ -0,0 +1,29 @@
+
+
+
+
+
+
+ Blog on Rails
+
+
+
+<% @post.errors.full_messages.each do |msg| %>
+ <%= msg %>
+
+<% end %>
+
+<%= form_with model: @post do |form| %>
+ <%= form.label :title %>
+
+ <%= form.text_field :title %>
+
+ <%= form.label :body %>
+
+ <%= form.text_area :body, size: "60x10" %>
+
+ <%= form.submit %>
+<% end %>
+
+
+
diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb
new file mode 100644
index 0000000..484282a
--- /dev/null
+++ b/app/views/posts/index.html.erb
@@ -0,0 +1,21 @@
+
+
+
+
+
+
+ Blog on Rails
+
+
+<% flash.each do |type, msg| %>
+ <%= msg %>
+
+<% end %>
+
+<% @posts.each do |post| %>
+ <%= post.title %>
+ <%= post.body %>
+<% end %>
+
+
+
diff --git a/app/views/posts/new.html.erb b/app/views/posts/new.html.erb
new file mode 100644
index 0000000..4046e9d
--- /dev/null
+++ b/app/views/posts/new.html.erb
@@ -0,0 +1,29 @@
+
+
+
+
+
+
+ Blog on Rails
+
+
+
+<% @post.errors.full_messages.each do |msg| %>
+ <%= msg %>
+
+<% end %>
+
+<%= form_with model: @post do |form| %>
+ <%= form.label :title %>
+
+ <%= form.text_field :title %>
+
+ <%= form.label :body %>
+
+ <%= form.text_area :body, size: "60x10" %>
+
+ <%= form.submit %>
+<% end %>
+
+
+
diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb
new file mode 100644
index 0000000..e6039ea
--- /dev/null
+++ b/app/views/posts/show.html.erb
@@ -0,0 +1,25 @@
+
+
+
+
+
+
+ Blog on Rails
+
+
+
+<% flash.each do |type, msg| %>
+ <%= msg %>
+
+<% end %>
+
+<% if @post %>
+ <%= @post.title %>
+
+ <%= @post.body %>
+ <%= link_to 'Edit', edit_post_path(@post) %>
+ <%= link_to 'Delete', post_path(@post), data: { turbo_method: :delete, turbo_confirm: 'Are you sure?' } %>
+<% end %>
+
+
+
diff --git a/config/routes.rb b/config/routes.rb
index 262ffd5..3c22144 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -3,4 +3,6 @@
# Defines the root path route ("/")
# root "articles#index"
+
+ resources :posts
end
From 8d0ba71f72f92756d0b924e579663195c6460065 Mon Sep 17 00:00:00 2001
From: Ibrahim <98372964+abe2dev@users.noreply.github.com>
Date: Mon, 14 Mar 2022 18:05:47 -0700
Subject: [PATCH 04/12] Create Comments model
---
app/controllers/posts_controller.rb | 2 +-
app/models/comment.rb | 5 +++++
app/models/post.rb | 2 ++
app/views/posts/index.html.erb | 7 +++++--
db/migrate/20220315002639_create_comments.rb | 10 ++++++++++
db/schema.rb | 11 ++++++++++-
6 files changed, 33 insertions(+), 4 deletions(-)
create mode 100644 app/models/comment.rb
create mode 100644 db/migrate/20220315002639_create_comments.rb
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index efb6808..4b603c2 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -39,7 +39,7 @@ def edit
def update
@post = Post.find params[:id]
if @post.update params.require(:post).permit(:title, :body)
- redirect_to post_path(@post)
+ redirect_to post_path(@post), { notice: "Post updated successfully", status: 303 }
else
render :edit, status:303
end
diff --git a/app/models/comment.rb b/app/models/comment.rb
new file mode 100644
index 0000000..cad758d
--- /dev/null
+++ b/app/models/comment.rb
@@ -0,0 +1,5 @@
+class Comment < ApplicationRecord
+ belongs_to :post
+
+ validates :body, presence: true, length: {maximum:255}
+end
diff --git a/app/models/post.rb b/app/models/post.rb
index 9096ad6..63cb440 100644
--- a/app/models/post.rb
+++ b/app/models/post.rb
@@ -1,4 +1,6 @@
class Post < ApplicationRecord
+ has_many :comments, dependent: :destroy
+
validates :title, presence: true, uniqueness: true
validates :body, presence: true, length: {minimum:50}
end
\ No newline at end of file
diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb
index 484282a..d31d47d 100644
--- a/app/views/posts/index.html.erb
+++ b/app/views/posts/index.html.erb
@@ -7,14 +7,17 @@
Blog on Rails
+
+<%= link_to 'new', new_post_path %>
+
<% flash.each do |type, msg| %>
<%= msg %>
<% end %>
<% @posts.each do |post| %>
- <%= post.title %>
- <%= post.body %>
+ <%= link_to post.title, post_path(post) %>
+
<% end %>
diff --git a/db/migrate/20220315002639_create_comments.rb b/db/migrate/20220315002639_create_comments.rb
new file mode 100644
index 0000000..e1ac60a
--- /dev/null
+++ b/db/migrate/20220315002639_create_comments.rb
@@ -0,0 +1,10 @@
+class CreateComments < ActiveRecord::Migration[7.0]
+ def change
+ create_table :comments do |t|
+ t.string :body
+ t.references :post, null: false, foreign_key: true
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index dc73aae..e4be264 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,10 +10,18 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[7.0].define(version: 2022_03_12_205739) do
+ActiveRecord::Schema[7.0].define(version: 2022_03_15_002639) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
+ create_table "comments", force: :cascade do |t|
+ t.string "body"
+ t.bigint "post_id", null: false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["post_id"], name: "index_comments_on_post_id"
+ end
+
create_table "posts", force: :cascade do |t|
t.string "title"
t.text "body"
@@ -21,4 +29,5 @@
t.datetime "updated_at", null: false
end
+ add_foreign_key "comments", "posts"
end
From aa3e18bc6509add32f249de02ddfa3aa4a4cb2b6 Mon Sep 17 00:00:00 2001
From: Ibrahim <98372964+abe2dev@users.noreply.github.com>
Date: Mon, 14 Mar 2022 21:00:58 -0700
Subject: [PATCH 05/12] Create Comments controller, no views and only two
RESTful routes for create and destroy actions
---
app/controllers/comments_controller.rb | 20 ++++++++++++++++++++
app/controllers/posts_controller.rb | 2 ++
app/helpers/comments_helper.rb | 2 ++
app/views/posts/show.html.erb | 20 ++++++++++++++++++++
config/routes.rb | 4 +++-
5 files changed, 47 insertions(+), 1 deletion(-)
create mode 100644 app/controllers/comments_controller.rb
create mode 100644 app/helpers/comments_helper.rb
diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb
new file mode 100644
index 0000000..10ebfe3
--- /dev/null
+++ b/app/controllers/comments_controller.rb
@@ -0,0 +1,20 @@
+class CommentsController < ApplicationController
+ def create
+ @post = Post.find params[:post_id]
+ @comment = Comment.new params.require(:comment).permit(:body)
+ @comment.post = @post
+ if @comment.save
+ redirect_to post_path(@post)
+ else
+ @comments = Comment.all
+ render 'posts/show', status: 303
+ end
+ end
+
+ def destroy
+ @comment = Comment.find params[:id]
+ @comment.destroy
+ @post = Post.find params[:post_id]
+ redirect_to post_path(@post), status: 303
+ end
+end
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index 4b603c2..0257775 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -5,6 +5,8 @@ def index
def show
@post = Post.find params[:id]
+ @comment = Comment.new
+ @comments = Comment.all
rescue => e
redirect_to posts_path, alert: e.message
end
diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb
new file mode 100644
index 0000000..0ec9ca5
--- /dev/null
+++ b/app/helpers/comments_helper.rb
@@ -0,0 +1,2 @@
+module CommentsHelper
+end
diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb
index e6039ea..cbbc8af 100644
--- a/app/views/posts/show.html.erb
+++ b/app/views/posts/show.html.erb
@@ -21,5 +21,25 @@
<%= link_to 'Delete', post_path(@post), data: { turbo_method: :delete, turbo_confirm: 'Are you sure?' } %>
<% end %>
+
+<% @comment&.errors&.full_messages.each do |msg| %>
+ <%= msg %>
+
+<% end %>
+
+<%= form_with model: @comment, url: post_comments_path(@post) do |form| %>
+ <%= form.label "Comment" %>
+
+ <%= form.text_area :body, size: "60x3" %>
+
+ <%= form.submit :Submit %>
+<% end %>
+
+<% @comments&.each do |comment| %>
+ <%= comment.body %>
+ <%= link_to 'Delete', post_comment_path(@post, comment), data: { turbo_method: :delete, turbo_confirm: 'Are you sure?' } %>
+
+<% end %>
+