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 %> + diff --git a/config/routes.rb b/config/routes.rb index 3c22144..a1aa78a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,5 +4,7 @@ # Defines the root path route ("/") # root "articles#index" - resources :posts + resources :posts do + resources :comments, only: [:create, :destroy] + end end From 4244bb1b6f7110567ace27f29fa8469e7d1c5f08 Mon Sep 17 00:00:00 2001 From: Ibrahim <98372964+abe2dev@users.noreply.github.com> Date: Mon, 14 Mar 2022 22:02:25 -0700 Subject: [PATCH 06/12] Add navigation elements, root route, missing information in views, resuce handlers for controllers actions --- app/controllers/comments_controller.rb | 7 ++++++- app/controllers/posts_controller.rb | 14 +++++++------- app/views/layouts/application.html.erb | 6 ++++++ app/views/posts/index.html.erb | 3 +-- app/views/posts/show.html.erb | 3 +++ config/routes.rb | 1 + 6 files changed, 24 insertions(+), 10 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 10ebfe3..8a87f2b 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -6,9 +6,11 @@ def create if @comment.save redirect_to post_path(@post) else - @comments = Comment.all + @comments = Comment.order(created_at: :desc) render 'posts/show', status: 303 end + rescue => e + redirect_to root_path, alert: e.message end def destroy @@ -16,5 +18,8 @@ def destroy @comment.destroy @post = Post.find params[:post_id] redirect_to post_path(@post), status: 303 + rescue => e + redirect_to root_path, alert: e.message end + end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 0257775..5971c2e 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,22 +1,22 @@ class PostsController < ApplicationController def index - @posts = Post.all + @posts = Post.order(created_at: :desc) end def show @post = Post.find params[:id] @comment = Comment.new - @comments = Comment.all + @comments = Comment.order(created_at: :desc) rescue => e - redirect_to posts_path, alert: e.message + redirect_to root_path, alert: e.message end def destroy @post = Post.find params[:id] @post.destroy - redirect_to posts_path, { notice: "Post deleted successfully", status: 303 } + redirect_to root_path, { notice: "Post deleted successfully", status: 303 } rescue => e - redirect_to posts_path, alert: e.message + redirect_to root_path, alert: e.message end def new @@ -35,7 +35,7 @@ def create def edit @post = Post.find params[:id] rescue => e - redirect_to posts_path, alert: e.message + redirect_to root_path, alert: e.message end def update @@ -46,6 +46,6 @@ def update render :edit, status:303 end rescue => e - redirect_to posts_path, alert: e.message + redirect_to root_path, alert: e.message end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 344c627..cf83321 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,6 +11,12 @@ + + <%= link_to 'Blog on Rails', root_path %> + <%= link_to 'Home', root_path %> + <%= link_to 'New Post', new_post_path %> +
+ <%= yield %> diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index d31d47d..278efd2 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -8,8 +8,6 @@ -<%= link_to 'new', new_post_path %> -
<% flash.each do |type, msg| %> <%= msg %>
@@ -17,6 +15,7 @@ <% @posts.each do |post| %> <%= link_to post.title, post_path(post) %> + <%= post.body %>
<% end %> diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index cbbc8af..448adeb 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -17,6 +17,8 @@ <%= @post.title %>
<%= @post.body %> + <%= time_ago_in_words(@post.created_at) %> ago +
<%= link_to 'Edit', edit_post_path(@post) %> <%= link_to 'Delete', post_path(@post), data: { turbo_method: :delete, turbo_confirm: 'Are you sure?' } %> <% end %> @@ -37,6 +39,7 @@ <% @comments&.each do |comment| %> <%= comment.body %> + <%= time_ago_in_words(comment.created_at) %> ago <%= link_to 'Delete', post_comment_path(@post, comment), data: { turbo_method: :delete, turbo_confirm: 'Are you sure?' } %>
<% end %> diff --git a/config/routes.rb b/config/routes.rb index a1aa78a..089c181 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,7 @@ # Defines the root path route ("/") # root "articles#index" + root to: 'posts#index' resources :posts do resources :comments, only: [:create, :destroy] From 48cd672ba153cb0a64d709e084adba437fb7cfec Mon Sep 17 00:00:00 2001 From: Ibrahim <98372964+abe2dev@users.noreply.github.com> Date: Wed, 16 Mar 2022 23:16:50 -0700 Subject: [PATCH 07/12] Styling using custom CSS --- .byebug_history | 9 +++ Gemfile | 3 + Gemfile.lock | 2 + app/assets/images/favicon.ico | Bin 0 -> 1926 bytes app/assets/stylesheets/application.css | 56 ++++++++++++++ app/assets/stylesheets/comment.css | 45 +++++++++++ app/assets/stylesheets/post.css | 99 +++++++++++++++++++++++++ app/assets/stylesheets/reset.css | 48 ++++++++++++ app/controllers/comments_controller.rb | 2 +- app/controllers/posts_controller.rb | 2 +- app/views/layouts/application.html.erb | 24 ++++-- app/views/posts/_form.html.erb | 13 ++++ app/views/posts/edit.html.erb | 30 +------- app/views/posts/index.html.erb | 29 ++------ app/views/posts/new.html.erb | 30 +------- app/views/posts/show.html.erb | 69 +++++++---------- 16 files changed, 330 insertions(+), 131 deletions(-) create mode 100644 .byebug_history create mode 100644 app/assets/images/favicon.ico create mode 100644 app/assets/stylesheets/comment.css create mode 100644 app/assets/stylesheets/post.css create mode 100644 app/assets/stylesheets/reset.css create mode 100644 app/views/posts/_form.html.erb diff --git a/.byebug_history b/.byebug_history new file mode 100644 index 0000000..0c6fb8c --- /dev/null +++ b/.byebug_history @@ -0,0 +1,9 @@ +continue +j`qqqldfjkdfj +continue +@comment.errors.full_messages_for(:body).join(", ") +@comment.error.full_messages_for(:body).join +@comment.error.full_messages_for(body) +continue +contunue +e diff --git a/Gemfile b/Gemfile index 0680cc6..97dcab6 100644 --- a/Gemfile +++ b/Gemfile @@ -51,6 +51,9 @@ gem "bootsnap", require: false group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem gem "debug", platforms: %i[ mri mingw x64_mingw ] + + gem 'byebug' + end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index a80008f..d8e227f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -70,6 +70,7 @@ GEM bootsnap (1.11.1) msgpack (~> 1.2) builder (3.2.4) + byebug (11.1.3) coderay (1.1.3) concurrent-ruby (1.1.9) crass (1.0.6) @@ -195,6 +196,7 @@ PLATFORMS DEPENDENCIES bootsnap + byebug debug faker importmap-rails diff --git a/app/assets/images/favicon.ico b/app/assets/images/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..b63d10b656e690ba226eb3e9a30d12b4747bdbeb GIT binary patch literal 1926 zcmV;12YL93P)p0001fP)t-s00030 z|Nk~NHW?Wi@bK`0f`X~3srmW&xw*Oa_V)bz{5m>1e}8|ov$I-STD-ixoSd9LKR=3! zipj~z(b3UiVPV(T*B>7r?Ck90B_X2Qb4M@L5#6clf7Z!Rt_ z-QC@Fb#+BWMINH zeg79+W1A`oBtVASaDMJ;%K4JuzyL$2kZd!Uoj?BlB_{aW?@i|EG7Wp*IIY4V+nZuhu*Dty8>xO^xT6-ruUXU|6?lKYGM0H-UpBYktWXAkGMykwSc4WSUZIu<4n&zL3 zx&IRqb#|KOkBs>Qwoz7?Xqq#L`MeL?C@Y+Dz3(wWdl+*uRrD6NpO~byuSu0=^d=3T zg74riv6$Ak*nygVnNt})wZ3&`%x&&fheL0D&^u%P0o*1#=BBQ;x%2XB!I+bDc3J4)aW-;W*57 z_AVhY*Lcy9m}~4&L}IQnzQ$s%z84vbx%&QdH0H{E92#?~IzNHOT=h-IW3HMRLu9U) zYC&YKcy1IXbNa6GEmY=IW#%21d9D!8;4-JVe69qUIZdS3^IlB@mFA4LuDtFK&eC#l2H*c*R73Ti)O{@7A_XGDp7Id*k-e%4@ z3c7~#GOHSF1y#U*&EL8IT_MB`=yQR28JskQZZ}L6rZdf=+5Abenxk6@EryNe{K+Q{ z5cNImvgg;lFU_S3(TDfAFB;7i>Ir#KQdxmMZ8BG>K_6NGyK6G9<_#M$R%zA{>jraP z$&CiWIvejg!6aBGBNMva6IKM9h|Sf@z5Pml4PLf-uH6*ySBob zdp;${OfdnQ4}DHGvBy^eoBRB(R}$gi=H8CgtD{eNb5DQQYdJCl=5*~}KlfapQ#4`j zU&RXE1d|zan)uYwJ#ZI37&52y&6lX=%&lH*(45kZ5BL39xZcB{`Q@2Uo%eOZ^^0cB zFRasv)5;z0%nRP8^ZnLQv1GUASF$_-Mk-?Rz}x^bo8@tu%hX9gYA%uAz-r!pa4A}I zdW_aQYuPr9*Ssx@i`bmLHJXbTK!k7JJO_l@ym7HLZu6#y2Dy2|R5x~Waas$#`S2L4 zIn4#@bnu(gHGk?S0&_u$;uy^N5ls~4+{{iK=DalSK=@%)=A4Ko7W4Y<;AqTi`^V!k zPfoF#S9i%qWL}AAVlrn_FXEll2+f&@CNA^))JtUMciTl{Grz6Bjm~^nE>sX5^M%%& zuKTf?mk~{b=5!gY`FH9iO7m;#B~J6=M5{>6={{O>+Kbja5g(TEA7g!b+IQ%9tpET3 M07*qoM6N<$f|HBkz5oCK literal 0 HcmV?d00001 diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 288b9ab..9fd26f1 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -10,6 +10,62 @@ * files in this directory. Styles in this file should be added after the last require_* statement. * It is generally better to create a new file per style scope. * + *= require reset + *= require post + *= require comment *= require_tree . *= require_self */ + +a { + text-decoration: none; + color: blue; +} + +header { + padding: 15px; + background-color: rgb(236, 241, 245); +} + +nav { + display: flex; + justify-content: space-between; + align-items: baseline; +} + +nav > .brand > a { + font-size: x-large; + color: blue; +} + +nav > .menu { + font-size: medium; +} + +nav > ul { + display: flex; + list-style: none; +} + +nav > ul > li > a { + margin-left: 10px; + color: blue; +} + +input[type=submit], button { + color: blue; + border-color: blue; + border-radius: 5px; + background-color: white; + font: arial; + font-size: 15px; + height: 35px; +} + + + + + + + + diff --git a/app/assets/stylesheets/comment.css b/app/assets/stylesheets/comment.css new file mode 100644 index 0000000..07b9be0 --- /dev/null +++ b/app/assets/stylesheets/comment.css @@ -0,0 +1,45 @@ +.new.comment { + padding: 15px; + display: flex; + flex-direction: column; +} + + +.new.comment > label, +.field_with_errors > textarea { + font-size: large; + font-weight: bold; +} + +.new.comment > textarea, +.field_with_errors > textarea { + margin: 10px 0; + width: 100%; +} + +.new.comment>p { + margin-bottom: 10px; + color: red; +} + +.new.comment > input { + padding: 5px; + width: 60px; +} + +.comments { + padding: 15px; +} + +.comment { + margin-bottom: 10px; +} + +.comment > .body { + word-break: break-all; +} + +.comment > .date { + margin-top: 5px; + font-size: x-small; +} diff --git a/app/assets/stylesheets/post.css b/app/assets/stylesheets/post.css new file mode 100644 index 0000000..c5b4056 --- /dev/null +++ b/app/assets/stylesheets/post.css @@ -0,0 +1,99 @@ +/* Index view */ +.main { + padding: 15px; + display: flex; + flex-direction: column; +} + +.posts > .post { + margin: 15px; + padding: 15px; + display: flex; + flex-direction: column; + border: 1px solid lightgray; +} + +.posts > .post > a { + padding-bottom: 5px; + font-size: x-large; + font-weight: bold; + width: auto; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +.posts > .post > p { + width: auto; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; +} + +/* Show view */ +main.post { + padding: 15px; + display: flex; + flex-direction: column; +} + +.post > .buttons { + display: flex; +} + +.buttons > form > button { + margin-right: 5px; + margin-top: 10px; + padding: 5px; +} + +.post > .details > .title { + font-size: x-large; + font-weight: bold; + word-break: break-all; +} + +.post > .details > .body { + margin-top: 10px; + font-size: medium; + word-break: break-all; +} + +.post > .details > .date { + margin-top: 15px; + font-size: x-small; +} + +/* New/Edit form view */ +.form-post { + padding: 15px; +} + +.input-group { + display: flex; + flex-direction: column; + margin: 10px 0; +} + +.input-group>label { + margin: 10px 0; +} + + + + + + + + + + + + + + + + + + + diff --git a/app/assets/stylesheets/reset.css b/app/assets/stylesheets/reset.css new file mode 100644 index 0000000..af94440 --- /dev/null +++ b/app/assets/stylesheets/reset.css @@ -0,0 +1,48 @@ +/* http://meyerweb.com/eric/tools/css/reset/ + v2.0 | 20110126 + License: none (public domain) +*/ + +html, body, div, span, applet, object, iframe, +h1, h2, h3, h4, h5, h6, p, blockquote, pre, +a, abbr, acronym, address, big, cite, code, +del, dfn, em, img, ins, kbd, q, s, samp, +small, strike, strong, sub, sup, tt, var, +b, u, i, center, +dl, dt, dd, ol, ul, li, +fieldset, form, label, legend, +table, caption, tbody, tfoot, thead, tr, th, td, +article, aside, canvas, details, embed, +figure, figcaption, footer, header, hgroup, +menu, nav, output, ruby, section, summary, +time, mark, audio, video { + margin: 0; + padding: 0; + border: 0; + font-size: 100%; + font: inherit; + vertical-align: baseline; +} +/* HTML5 display-role reset for older browsers */ +article, aside, details, figcaption, figure, +footer, header, hgroup, menu, nav, section { + display: block; +} +body { + line-height: 1; +} +ol, ul { + list-style: none; +} +blockquote, q { + quotes: none; +} +blockquote:before, blockquote:after, +q:before, q:after { + content: ''; + content: none; +} +table { + border-collapse: collapse; + border-spacing: 0; +} \ No newline at end of file diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 8a87f2b..0422fd1 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -6,7 +6,7 @@ def create if @comment.save redirect_to post_path(@post) else - @comments = Comment.order(created_at: :desc) + @comments = @post.comments.order(created_at: :desc) render 'posts/show', status: 303 end rescue => e diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 5971c2e..988e9b2 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -6,7 +6,7 @@ def index def show @post = Post.find params[:id] @comment = Comment.new - @comments = Comment.order(created_at: :desc) + @comments = @post.comments.order(created_at: :desc) rescue => e redirect_to root_path, alert: e.message end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index cf83321..e6e9d54 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,22 +1,32 @@ - BlogOnRails + Blog On Rails <%= csrf_meta_tags %> <%= csp_meta_tag %> + <%= favicon_link_tag 'favicon.ico' %> <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %> <%= javascript_importmap_tags %> - - <%= link_to 'Blog on Rails', root_path %> - <%= link_to 'Home', root_path %> - <%= link_to 'New Post', new_post_path %> -
- +
+ +
<%= yield %> diff --git a/app/views/posts/_form.html.erb b/app/views/posts/_form.html.erb new file mode 100644 index 0000000..a209d0b --- /dev/null +++ b/app/views/posts/_form.html.erb @@ -0,0 +1,13 @@ +<%= 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/edit.html.erb b/app/views/posts/edit.html.erb index 4046e9d..3e31518 100644 --- a/app/views/posts/edit.html.erb +++ b/app/views/posts/edit.html.erb @@ -1,29 +1 @@ - - - - - - - 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 %> - - - +<%= render "form" %> \ No newline at end of file diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 278efd2..8db7e7a 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -1,23 +1,8 @@ - - - - - - - Blog on Rails - - - -<% flash.each do |type, msg| %> - <%= msg %> -
+
+<% @posts.each do |post| %> +
+ <%= link_to post.title, post_path(post) %> +

<%= post.body %>

+
<% end %> - -<% @posts.each do |post| %> - <%= link_to post.title, post_path(post) %> - <%= post.body %> -
-<% end %> - - - +
\ No newline at end of file diff --git a/app/views/posts/new.html.erb b/app/views/posts/new.html.erb index 4046e9d..3e31518 100644 --- a/app/views/posts/new.html.erb +++ b/app/views/posts/new.html.erb @@ -1,29 +1 @@ - - - - - - - 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 %> - - - +<%= render "form" %> \ No newline at end of file diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 448adeb..5b951aa 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -1,48 +1,33 @@ - - - - - - - Blog on Rails - - - -<% flash.each do |type, msg| %> - <%= msg %> -
-<% end %> - <% if @post %> - <%= @post.title %> -
- <%= @post.body %> - <%= time_ago_in_words(@post.created_at) %> ago -
- <%= link_to 'Edit', edit_post_path(@post) %> - <%= link_to 'Delete', post_path(@post), data: { turbo_method: :delete, turbo_confirm: 'Are you sure?' } %> -<% end %> - -
-<% @comment&.errors&.full_messages.each do |msg| %> - <%= msg %> -
+
+
+

<%= @post.title %>

+

<%= @post.body %>

+

Posted <%= time_ago_in_words(@post.created_at) %> ago

+
+
+ <%= button_to 'Edit', edit_post_path(@post), method: :get %> + <%= button_to 'Delete', post_path(@post), method: :delete %> +
+
<% 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 %> - <%= time_ago_in_words(comment.created_at) %> ago - <%= link_to 'Delete', post_comment_path(@post, comment), data: { turbo_method: :delete, turbo_confirm: 'Are you sure?' } %> -
+
+ <%= form.label :body, "Comment" %> + <%= form.text_area :body, maxlenght: "255", rows: "5" %> + <% if @comment.errors.any? %> +

<%= @comment.errors.full_messages.join(", ") %>

+ <% end %> + <%= form.submit :Submit %> +
<% end %> - - +
+ <% @comments&.each do |comment| %> +
+

<%= comment.body %>

+

Commmented <%= time_ago_in_words(comment.created_at) %> ago ⦁ <%= link_to 'Delete', post_comment_path(@post, comment), data: { turbo_method: :delete } %>

+
+ <% end %> +
From d521542c5f38ac5c0e3eaf899c8ef0d0411ac8a0 Mon Sep 17 00:00:00 2001 From: Ibrahim <98372964+abe2dev@users.noreply.github.com> Date: Fri, 18 Mar 2022 14:06:01 -0700 Subject: [PATCH 08/12] Semantic HTML elements and CSS --- app/assets/stylesheets/application.css | 41 ++++++++----- app/assets/stylesheets/comment.css | 35 ++++++----- app/assets/stylesheets/post.css | 75 ++++++++++++----------- app/assets/stylesheets/reset.css | 8 ++- app/controllers/comments_controller.rb | 36 +++++------ app/controllers/posts_controller.rb | 82 ++++++++++++++------------ app/views/layouts/application.html.erb | 12 ++-- app/views/posts/_form.html.erb | 30 ++++++---- app/views/posts/index.html.erb | 18 +++--- app/views/posts/show.html.erb | 54 ++++++++--------- 10 files changed, 216 insertions(+), 175 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 9fd26f1..f68bba7 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -17,14 +17,14 @@ *= require_self */ -a { + a { text-decoration: none; color: blue; } header { padding: 15px; - background-color: rgb(236, 241, 245); + background-color: lightgray; } nav { @@ -38,21 +38,18 @@ nav > .brand > a { color: blue; } -nav > .menu { - font-size: medium; -} - nav > ul { display: flex; list-style: none; } nav > ul > li > a { - margin-left: 10px; + margin-left: 20px; color: blue; } -input[type=submit], button { +input[type="submit"], +button { color: blue; border-color: blue; border-radius: 5px; @@ -62,10 +59,26 @@ input[type=submit], button { height: 35px; } +@media only screen and (max-width: 960px) { + .posts { + display: flex; + flex-wrap: wrap; + } + + article { + flex-grow: 1; + width: 50%; + } +} - - - - - - +@media only screen and (max-width: 600px) { + .posts { + display: flex; + flex-wrap: wrap; + } + + article { + flex-grow: 1; + width: 100%; + } +} diff --git a/app/assets/stylesheets/comment.css b/app/assets/stylesheets/comment.css index 07b9be0..5dc5cfc 100644 --- a/app/assets/stylesheets/comment.css +++ b/app/assets/stylesheets/comment.css @@ -1,30 +1,33 @@ -.new.comment { +.comment-form { padding: 15px; display: flex; flex-direction: column; } +.comment-form > .field_with_errors { + width: 100%; +} -.new.comment > label, -.field_with_errors > textarea { - font-size: large; - font-weight: bold; +.comment-form > label, +.comment-form > .field_with_errors > label { + font-size: large; + font-weight: bold; } -.new.comment > textarea, -.field_with_errors > textarea { - margin: 10px 0; - width: 100%; +.comment-form > textarea, +.comment-form > .field_with_errors > textarea { + margin: 10px 0; + width: 100%; } -.new.comment>p { - margin-bottom: 10px; - color: red; +.comment-form > p { + margin-bottom: 10px; + color: red; } -.new.comment > input { - padding: 5px; - width: 60px; +.comment-form > input { + padding: 5px; + width: 100px; } .comments { @@ -42,4 +45,4 @@ .comment > .date { margin-top: 5px; font-size: x-small; -} +} \ No newline at end of file diff --git a/app/assets/stylesheets/post.css b/app/assets/stylesheets/post.css index c5b4056..41b7dd4 100644 --- a/app/assets/stylesheets/post.css +++ b/app/assets/stylesheets/post.css @@ -1,19 +1,23 @@ /* Index view */ -.main { - padding: 15px; +.posts { display: flex; - flex-direction: column; + flex-wrap: wrap; +} + +article { + flex-grow: 1; + width: 33%; } -.posts > .post { - margin: 15px; +.posts > .post > .details { + margin: 10px; padding: 15px; display: flex; flex-direction: column; - border: 1px solid lightgray; + border: 1px solid lightgrey; } -.posts > .post > a { +.posts > .post > .details > a { padding-bottom: 5px; font-size: x-large; font-weight: bold; @@ -23,7 +27,7 @@ text-overflow: ellipsis; } -.posts > .post > p { +.posts > .post > .details > p { width: auto; white-space: nowrap; overflow: hidden; @@ -37,63 +41,66 @@ main.post { flex-direction: column; } -.post > .buttons { +main.post > .buttons { display: flex; } -.buttons > form > button { +main.post > .buttons > form > button { margin-right: 5px; margin-top: 10px; padding: 5px; } -.post > .details > .title { +main.post > .details > .title { font-size: x-large; font-weight: bold; word-break: break-all; } -.post > .details > .body { +main.post > .details > .body { margin-top: 10px; font-size: medium; word-break: break-all; } -.post > .details > .date { +main.post > .details > .date { margin-top: 15px; font-size: x-small; } -/* New/Edit form view */ -.form-post { +/* New/Edit form views */ +.post-form { padding: 15px; + margin: 10px 0; } -.input-group { +.post-form > .input-group { display: flex; flex-direction: column; - margin: 10px 0; } -.input-group>label { - margin: 10px 0; +.post-form > .input-group > .field_with_errors { + display: flex; + width: 100%; } +.post-form > .input-group > label, +.post-form > .input-group > .field_with_errors > label { + margin: 10px 0; +} +.post-form > .input-group > .field_with_errors > input[type="text"] { + margin: 10px 0; + width: 100%; +} +.post-form > .input-group > textarea, +.post-form > .input-group > .field_with_errors > textarea { + margin-bottom: 10px; + width: 100%; +} - - - - - - - - - - - - - - - +.post-form > .input-group > p { + margin-bottom: 10px; + color: red; +} \ No newline at end of file diff --git a/app/assets/stylesheets/reset.css b/app/assets/stylesheets/reset.css index af94440..8c6087e 100644 --- a/app/assets/stylesheets/reset.css +++ b/app/assets/stylesheets/reset.css @@ -45,4 +45,10 @@ q:before, q:after { table { border-collapse: collapse; border-spacing: 0; -} \ No newline at end of file +} + +*, +*::before, +*::after { + box-sizing: border-box; +} diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 0422fd1..8a10f99 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,25 +1,25 @@ 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 = @post.comments.order(created_at: :desc) - render 'posts/show', status: 303 - end + @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 = @post.comments.order(created_at: :desc) + render 'posts/show', status: 303 + end rescue => e - redirect_to root_path, alert: e.message + redirect_to root_path, alert: e.message end - + def destroy - @comment = Comment.find params[:id] - @comment.destroy - @post = Post.find params[:post_id] - redirect_to post_path(@post), status: 303 + @comment = Comment.find params[:id] + @comment.destroy + @post = Post.find params[:post_id] + redirect_to post_path(@post), status: 303 rescue => e - redirect_to root_path, alert: e.message + redirect_to root_path, alert: e.message end - -end + end + \ No newline at end of file diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 988e9b2..02268a6 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,51 +1,55 @@ +# postsController class PostsController < ApplicationController - def index - @posts = Post.order(created_at: :desc) + def index + @posts = Post.order(created_at: :desc) end - + def show - @post = Post.find params[:id] - @comment = Comment.new - @comments = @post.comments.order(created_at: :desc) - rescue => e - redirect_to root_path, alert: e.message - end - + @post = Post.find params[:id] + @comment = Comment.new + @comments = @post.comments.order(created_at: :desc) + rescue StandardError => e + redirect_to root_path, alert: e.message + end + def destroy - @post = Post.find params[:id] - @post.destroy - redirect_to root_path, { notice: "Post deleted successfully", status: 303 } - rescue => e - redirect_to root_path, alert: e.message - end - + @post = Post.find params[:id] + @post.destroy + redirect_to posts_path, { notice: 'Post deleted successfully', status: 303 } + rescue StandardError => e + redirect_to root_path, alert: e.message + end + def new - @post = Post.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 + @post = Post.new params.require(:post).permit(:title, :body) + if @post.save + redirect_to post_path(@post) #{ status: 303, notice: 'Post created successfully' } + else + render :new, status: 303 + end end - + def edit - @post = Post.find params[:id] - rescue => e - redirect_to root_path, alert: e.message + @post = Post.find params[:id] + rescue StandardError => e + redirect_to root_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), { notice: "Post updated successfully", status: 303 } - else - render :edit, status:303 - end - rescue => e - redirect_to root_path, alert: e.message + @post = Post.find params[:id] + puts @post + if @post.update params.require(:post).permit(:title, :body) + puts @post + redirect_to post_path(@post), { status: 303, notice: 'Post updated successfully' } + else + render :edit, status: 303 + end + rescue StandardError => e + redirect_to posts_path, alert: e.message end -end + end + \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index e6e9d54..32b993e 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -5,7 +5,7 @@ <%= csrf_meta_tags %> <%= csp_meta_tag %> - <%= favicon_link_tag 'favicon.ico' %> + <%= favicon_link_tag 'favicon.ico'%> <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %> <%= javascript_importmap_tags %> @@ -14,8 +14,8 @@
-
+ + <%= yield %> - + \ No newline at end of file diff --git a/app/views/posts/_form.html.erb b/app/views/posts/_form.html.erb index a209d0b..96a8ec3 100644 --- a/app/views/posts/_form.html.erb +++ b/app/views/posts/_form.html.erb @@ -1,13 +1,19 @@ -<%= form_with model: @post do |form| %> -
-
- <%= form.label :title %> - <%= form.text_field :title %> -
-
- <%= form.label :body %> - <%= form.text_area :body, size: "60x10" %> -
- <%= form.submit %> + <%= form_with model: @post do |form| %> +
+
+ <%= form.label :title %> + <%= form.text_field :title %> + <% if @post.errors.any? %> +

<%= @post.errors.full_messages_for(:title).join(", ") %>

+ <% end %> +
+
+ <%= form.label :body %> + <%= form.text_area :body, size: "60x10" %> + <% if @post.errors.any? %> +

<%= @post.errors.full_messages_for(:body).join(", ") %>

+ <% end %> +
+ <%= form.submit %>
-<% end %> + <% end %> \ No newline at end of file diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 8db7e7a..abb16f1 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -1,8 +1,10 @@ -
-<% @posts.each do |post| %> -
- <%= link_to post.title, post_path(post) %> -

<%= post.body %>

-
-<% end %> -
\ No newline at end of file +
+ <% @posts.each do |post| %> +
+
+ <%= link_to post.title, post_path(post) %> +

<%= post.body %>

+
+
+ <% end %> +
\ No newline at end of file diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 5b951aa..698e795 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -1,33 +1,33 @@ -<% if @post %> -
-
-

<%= @post.title %>

-

<%= @post.body %>

-

Posted <%= time_ago_in_words(@post.created_at) %> ago

-
-
- <%= button_to 'Edit', edit_post_path(@post), method: :get %> - <%= button_to 'Delete', post_path(@post), method: :delete %> -
-
+<% if @post %> +
+
+

<%= @post.title %>

+

<%= @post.body %>

+

Posted <%= time_ago_in_words(@post.created_at) %> ago

+
+
+ <%= button_to 'Edit', edit_post_path(@post), method: :get %> + <%= button_to 'Delete',post_path(@post), method: :delete %> +
+
<% end %> - + <%= form_with model: @comment, url: post_comments_path(@post) do |form| %> -
- <%= form.label :body, "Comment" %> - <%= form.text_area :body, maxlenght: "255", rows: "5" %> - <% if @comment.errors.any? %> -

<%= @comment.errors.full_messages.join(", ") %>

- <% end %> - <%= form.submit :Submit %> -
+
+ <%= form.label :body, "Comment" %> + <%= form.text_area :body, maxlength: "255", rows: "5" %> + <% if @comment.errors.any? %> +

<%= @comment.errors.full_messages.join(", ") %>

+ <% end %> + <%= form.submit :Submit %> +
<% end %>
- <% @comments&.each do |comment| %> -
-

<%= comment.body %>

-

Commmented <%= time_ago_in_words(comment.created_at) %> ago ⦁ <%= link_to 'Delete', post_comment_path(@post, comment), data: { turbo_method: :delete } %>

-
- <% end %> + <% @comments&.each do |comment| %> +
+

<%= comment.body %>

+

Commented <%= time_ago_in_words(comment.created_at) %> ago • <%= link_to 'Delete', post_comment_path(@post, comment), data: { turbo_method: :delete } %>

+
+ <% end %>
From 79550eabce6a8eb6ef4a74378be6093e32e50c1f Mon Sep 17 00:00:00 2001 From: Ibrahim <98372964+abe2dev@users.noreply.github.com> Date: Sat, 19 Mar 2022 02:00:30 -0700 Subject: [PATCH 09/12] Update seed file --- db/seeds.rb | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index bc25fce..d5526c9 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,7 +1,32 @@ # This file should contain all the record creation needed to seed the database with its default values. -# The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup). +# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup). # # Examples: # -# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }]) -# Character.create(name: "Luke", movie: movies.first) +# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) +# Character.create(name: 'Luke', movie: movies.first) + +# First destroy all records from table comments due to FK constraint. +Comment.destroy_all +# Then destroy all records from table posts. +Post.destroy_all + +# Reset the primary key sequence to 1. +ActiveRecord::Base.connection.reset_pk_sequence!(:posts) +ActiveRecord::Base.connection.reset_pk_sequence!(:comments) + +# Bulk insert of 50 fake posts. +Post.insert_all( + 50.times.map do + { + title: Faker::Hacker.say_something_smart, + body: Faker::ChuckNorris.fact, + created_at: Faker::Time.backward(days: 365), + updated_at: DateTime.now + } + end +) + +# Show how many fake posts are in the table posts. +puts Cowsay.say("Generated #{Post.count} posts using Faker.", :frogs) +puts Cowsay.say("Comments cleared out - #{Comment.count} comments.", :tux) From f5585de16a4e603f55255a04422e846893ad2eb3 Mon Sep 17 00:00:00 2001 From: Ibrahim <98372964+abe2dev@users.noreply.github.com> Date: Sat, 19 Mar 2022 15:38:46 -0700 Subject: [PATCH 10/12] initialize Cowsay & updating seed file --- Gemfile | 43 +++++++++++++++++++++---------------------- Gemfile.lock | 29 ++++++++++++++++++++++------- 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/Gemfile b/Gemfile index 97dcab6..b230f65 100644 --- a/Gemfile +++ b/Gemfile @@ -1,31 +1,31 @@ -source "https://rubygems.org" +source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby "3.0.0" +ruby '3.0.0' # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" -gem "rails", "~> 7.0.2", ">= 7.0.2.3" +gem 'rails', '~> 7.0.2', '>= 7.0.2.3' # The original asset pipeline for Rails [https://github.com/rails/sprockets-rails] -gem "sprockets-rails" +gem 'sprockets-rails' # Use postgresql as the database for Active Record -gem "pg", "~> 1.1" +gem 'pg', '~> 1.1' # Use the Puma web server [https://github.com/puma/puma] -gem "puma", "~> 5.0" +gem 'puma', '~> 5.0' # Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails] -gem "importmap-rails" +gem 'importmap-rails' # Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev] -gem "turbo-rails" +gem 'turbo-rails' # Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev] -gem "stimulus-rails" +gem 'stimulus-rails' # Build JSON APIs with ease [https://github.com/rails/jbuilder] -gem "jbuilder" +gem 'jbuilder' # Use Redis adapter to run Action Cable in production # gem "redis", "~> 4.0" @@ -37,28 +37,30 @@ gem "jbuilder" # gem "bcrypt", "~> 3.1.7" # Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] +gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] # Reduces boot times through caching; required in config/boot.rb -gem "bootsnap", require: false +gem 'bootsnap', require: false # Use Sass to process CSS -# gem "sassc-rails" +gem 'sassc-rails' # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] # gem "image_processing", "~> 1.2" +gem 'bootstrap', '~> 5.1.3' + group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem - gem "debug", platforms: %i[ mri mingw x64_mingw ] + gem 'debug', platforms: %i[mri mingw x64_mingw] - gem 'byebug' - + gem 'cowsay' + gem 'faker' end group :development do # Use console on exceptions pages [https://github.com/rails/web-console] - gem "web-console" + gem 'web-console' # Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler] # gem "rack-mini-profiler" @@ -66,8 +68,5 @@ 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 - + gem 'byebug' +end \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index d8e227f..c815474 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -66,21 +66,29 @@ GEM i18n (>= 1.6, < 2) minitest (>= 5.1) tzinfo (~> 2.0) + autoprefixer-rails (10.4.2.0) + execjs (~> 2) bindex (0.8.1) bootsnap (1.11.1) msgpack (~> 1.2) + bootstrap (5.1.3) + autoprefixer-rails (>= 9.1.0) + popper_js (>= 2.9.3, < 3) + sassc-rails (>= 2.0.0) builder (3.2.4) byebug (11.1.3) - coderay (1.1.3) concurrent-ruby (1.1.9) + cowsay (0.3.0) crass (1.0.6) debug (1.4.0) irb (>= 1.3.6) reline (>= 0.2.7) digest (3.1.0) erubi (1.10.0) + execjs (2.8.1) faker (2.20.0) i18n (>= 1.8.11, < 2) + ffi (1.15.5) globalid (1.0.0) activesupport (>= 5.0) i18n (1.10.0) @@ -124,11 +132,7 @@ 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) + popper_js (2.9.3) puma (5.6.2) nio4r (~> 2.0) racc (1.6.0) @@ -164,6 +168,14 @@ GEM rake (13.0.6) reline (0.3.1) io-console (~> 0.5) + sassc (2.4.0) + ffi (~> 1.9) + sassc-rails (2.1.2) + railties (>= 4.0.0) + sassc (>= 2.0) + sprockets (> 3.0) + sprockets-rails + tilt sprockets (4.0.3) concurrent-ruby (~> 1.0) rack (> 1, < 3) @@ -175,6 +187,7 @@ GEM railties (>= 6.0.0) strscan (3.0.1) thor (1.2.1) + tilt (2.0.10) timeout (0.2.0) turbo-rails (1.0.1) actionpack (>= 6.0.0) @@ -196,15 +209,17 @@ PLATFORMS DEPENDENCIES bootsnap + bootstrap (~> 5.1.3) byebug + cowsay debug faker importmap-rails jbuilder pg (~> 1.1) - pry-rails (~> 0.3.9) puma (~> 5.0) rails (~> 7.0.2, >= 7.0.2.3) + sassc-rails sprockets-rails stimulus-rails turbo-rails From 22c81ebee450ab0e3240ae4aad2e6188325c50d8 Mon Sep 17 00:00:00 2001 From: Ibrahim <98372964+abe2dev@users.noreply.github.com> Date: Sat, 19 Mar 2022 17:49:10 -0700 Subject: [PATCH 11/12] Deleted README.md file --- README.md | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 7db80e4..0000000 --- a/README.md +++ /dev/null @@ -1,24 +0,0 @@ -# README - -This README would normally document whatever steps are necessary to get the -application up and running. - -Things you may want to cover: - -* Ruby version - -* System dependencies - -* Configuration - -* Database creation - -* Database initialization - -* How to run the test suite - -* Services (job queues, cache servers, search engines, etc.) - -* Deployment instructions - -* ... From be557b9fa833f349f3b510f43399ffffba2bacf4 Mon Sep 17 00:00:00 2001 From: Ibrahim MA Date: Tue, 22 Mar 2022 19:27:42 -0700 Subject: [PATCH 12/12] Update ruby to 3.1.1 --- .ruby-version | 2 +- Gemfile | 2 +- Gemfile.lock | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.ruby-version b/.ruby-version index 85588be..2a49746 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -ruby-3.0.0 +ruby-3.1.1 diff --git a/Gemfile b/Gemfile index b230f65..4107249 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby '3.0.0' +ruby '3.1.1' # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" gem 'rails', '~> 7.0.2', '>= 7.0.2.3' diff --git a/Gemfile.lock b/Gemfile.lock index c815474..386d7bd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -77,7 +77,7 @@ GEM sassc-rails (>= 2.0.0) builder (3.2.4) byebug (11.1.3) - concurrent-ruby (1.1.9) + concurrent-ruby (1.1.10) cowsay (0.3.0) crass (1.0.6) debug (1.4.0) @@ -103,7 +103,7 @@ GEM jbuilder (2.11.5) actionview (>= 5.0.0) activesupport (>= 5.0.0) - loofah (2.14.0) + loofah (2.15.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) mail (2.7.1) @@ -227,7 +227,7 @@ DEPENDENCIES web-console RUBY VERSION - ruby 3.0.0p0 + ruby 3.1.1p18 BUNDLED WITH 2.3.8