From 63e1a7473a4146a4239e38560064de7dcfc9fddb Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sun, 2 Jul 2023 00:36:16 +0300 Subject: [PATCH 01/16] update routes.rb to add comments and liks routes --- config/routes.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 82c2a70..9065499 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,9 @@ root "users#index" resources :users, only: [:index, :show] do - resources :posts, only: [:index, :show] + resources :posts, only: [:index, :show, :new, :create] do + resources :comments, only: [:new, :create] + post 'like', on: :member + end end end From a60181828fd9f1f89592dce0f433340bb4c9759d Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sun, 2 Jul 2023 00:36:56 +0300 Subject: [PATCH 02/16] add like and comment button --- app/views/posts/index.html.erb | 6 +++--- app/views/posts/show.html.erb | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 76bb528..bf1376b 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -1,8 +1,8 @@
-
-
- User Photo +
+
+ User Photo

<%= @post.text %>

+
+ <% if @current_user %> + <%= form_with(model: @post, url: like_user_post_path(user_id: @current_user.id, id: @post.id), method: :post, local: true) do |form| %> + <%= form.button type: :submit, class: 'like-button' do %> + Like + <% end %> + <% end %> + <% end %> +
@@ -31,5 +40,11 @@
<% end %>
+ +
From 7b81aeba90177088ba2006587e1b48c5b8999098 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sun, 2 Jul 2023 00:37:15 +0300 Subject: [PATCH 03/16] add current_user to ApplicationController --- app/controllers/application_controller.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d1..1d8dbda 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,5 @@ class ApplicationController < ActionController::Base + def current_user + @current_user ||= User.first + end end From f23a02d297c1d9784e1466785785db1ecdc451d5 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sun, 2 Jul 2023 00:37:53 +0300 Subject: [PATCH 04/16] update posts controller --- app/controllers/posts_controller.rb | 30 +++++++++++++++++++++++++++++ app/views/layouts/mailer.html.erb | 2 +- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 029b184..a2e9275 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -9,4 +9,34 @@ def show @post = @user.posts.find(params[:id]) @comments = @post.comments end + + def new + @post = Post.new + end + + def like + @post = Post.find(params[:id]) + @like = Like.new(author_id: params[:user_id], post_id: @post.id) + @like.save + redirect_to user_post_path + end + + def create + @post = Post.new(post_params) + @post.author_id = current_user.id + @post.comments_counter = 0 + @post.likes_counters = 0 + + if @post.save + redirect_to user_posts_path, notice: 'Post was successfully created.' + else + render 'new' + end + end + + private + + def post_params + params.require(:post).permit(:title, :text) + end end diff --git a/app/views/layouts/mailer.html.erb b/app/views/layouts/mailer.html.erb index cbd34d2..8eb4765 100644 --- a/app/views/layouts/mailer.html.erb +++ b/app/views/layouts/mailer.html.erb @@ -1,7 +1,7 @@ - + From 4fa59fcdca161684ba24ca4b3e0f190d04d2b5a8 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sun, 2 Jul 2023 00:38:19 +0300 Subject: [PATCH 05/16] create comment form --- app/views/comments/new.html.erb | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 app/views/comments/new.html.erb diff --git a/app/views/comments/new.html.erb b/app/views/comments/new.html.erb new file mode 100644 index 0000000..5a14028 --- /dev/null +++ b/app/views/comments/new.html.erb @@ -0,0 +1,13 @@ +
+
+

Add Comment

+ <%= form_with(model: @comment, url: user_post_comments_url(@post.author, @post), local: true) do |form| %> +
+ <%= form.text_area :text %> +
+
+ <%= form.submit 'Create Comment', class: 'comment-btn'%> +
+ <% end %> +
+
\ No newline at end of file From f1e9e48beddd5e0b3f64ccfa2a1414a6b490bfa2 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sun, 2 Jul 2023 00:38:37 +0300 Subject: [PATCH 06/16] create post form --- app/views/posts/new.html.erb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 app/views/posts/new.html.erb diff --git a/app/views/posts/new.html.erb b/app/views/posts/new.html.erb new file mode 100644 index 0000000..408a66e --- /dev/null +++ b/app/views/posts/new.html.erb @@ -0,0 +1,15 @@ +
+
+

Create New Post

+ <%= form_with(model: @post, url: user_posts_path, local: true) do |form| %> + <%= form.label :title %> + <%= form.text_field :title, name: 'post[title]' %> + <%= form.label :text %> + <%= form.text_area :text, name: 'post[text]' %> + <%= form.submit 'Create Post' %> + <% end %> +
+
+``` + +### \ No newline at end of file From 881991c07b15e2ae1c8940381ed5a6c745b3eb6d Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sun, 2 Jul 2023 00:39:00 +0300 Subject: [PATCH 07/16] create comments controller --- app/controllers/comments_controller.rb | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index aba64e2..caf345f 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,5 +1,26 @@ class CommentsController < ApplicationController - def index; end + def new + @comment = Comment.new + @user = User.find(params[:user_id]) + @post = Post.find(params[:post_id]) + end - def show; end + def create + @comment = Comment.new(comment_params) + @comment.author_id = current_user.id + @comment.post_id = params[:post_id] + + if @comment.save + redirect_to user_post_path(user_id: @comment.author_id, id: @comment.post_id), + notice: 'Comment was successfully created.' + else + render 'new' + end + end + + private + + def comment_params + params.require(:comment).permit(:content, :text) + end end From 9dc045737546ee9e842f693a656955912bc80e6c Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sun, 2 Jul 2023 00:39:15 +0300 Subject: [PATCH 08/16] create Likes controller --- app/controllers/likes_controller.rb | 66 ++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index 52d88b8..951deed 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -1,5 +1,69 @@ class LikesController < ApplicationController - def index; end + before_action :set_like, only: %i[show edit update destroy] + # GET /likes or /likes.json + def index + @likes = Like.all + end + + # GET /likes/1 or /likes/1.json def show; end + + # GET /likes/new + def new + @like = Like.new + end + + # GET /likes/1/edit + def edit; end + + # POST /likes or /likes.json + def create + @like = Like.new(like_params) + + respond_to do |format| + if @like.save + format.html { redirect_to like_url(@like), notice: 'Like was successfully created.' } + format.json { render :show, status: :created, location: @like } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @like.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /likes/1 or /likes/1.json + def update + respond_to do |format| + if @like.update(like_params) + format.html { redirect_to like_url(@like), notice: 'Like was successfully updated.' } + format.json { render :show, status: :ok, location: @like } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @like.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /likes/1 or /likes/1.json + def destroy + @like.destroy + + respond_to do |format| + format.html { redirect_to likes_url, notice: 'Like was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + + # Use callbacks to share common setup or constraints between actions. + def set_like + @like = Like.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def like_params + params.require(:like).permit(:AuthorId, :PostId, :UpdatedAt, :CreatedAt) + end end From 6f946cd4c9c3d459b38746ded9b610f4c3ec369e Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sun, 2 Jul 2023 00:39:34 +0300 Subject: [PATCH 09/16] add style for post and comment forms --- app/assets/stylesheets/application.css | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index c229302..c16540c 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -112,3 +112,33 @@ a { border-right: 3px solid black; border-radius: 5px; } + +.post-form, +.comment-form { + width: 60%; + display: flex; + flex-direction: column; + padding: 1rem; + gap: 1rem; + border: 3px solid black; +} + +.comments, +.posts { + width: 80%; + display: flex; + flex-direction: column; + padding: 1rem; + gap: 1rem; +} + +.comment-btn, +.like-button { + cursor: pointer; + padding: 5px 10px; + margin-left: 5rem; + font-size: 15px; + border-bottom: 5px solid black; + border-right: 3px solid black; + border-radius: 5px; +} \ No newline at end of file From 6c1a99cb48eda612f78718f657ad7d3caac9a0fc Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sun, 2 Jul 2023 00:47:30 +0300 Subject: [PATCH 10/16] fix stylelint error --- app/assets/stylesheets/application.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index c16540c..034caea 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -141,4 +141,4 @@ a { border-bottom: 5px solid black; border-right: 3px solid black; border-radius: 5px; -} \ No newline at end of file +} From 7415851310b0307713726bb0c61c3df90b493bc0 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 8 Jul 2023 21:42:33 +0300 Subject: [PATCH 11/16] debug and fix type errors --- app/controllers/comments_controller.rb | 4 ++-- app/controllers/posts_controller.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index caf345f..cf3ebcb 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -7,11 +7,11 @@ def new def create @comment = Comment.new(comment_params) - @comment.author_id = current_user.id + @comment.user_id = current_user.id @comment.post_id = params[:post_id] if @comment.save - redirect_to user_post_path(user_id: @comment.author_id, id: @comment.post_id), + redirect_to user_post_path(user_id: @comment.user_id, id: @comment.post_id), notice: 'Comment was successfully created.' else render 'new' diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index a2e9275..0414712 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -25,7 +25,7 @@ def create @post = Post.new(post_params) @post.author_id = current_user.id @post.comments_counter = 0 - @post.likes_counters = 0 + @post.likes_counter = 0 if @post.save redirect_to user_posts_path, notice: 'Post was successfully created.' From b007d9b8035ca944ac500e84cb30198ac4bcf291 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 8 Jul 2023 23:47:01 +0300 Subject: [PATCH 12/16] move def new for like from posts_controller to likes_controller --- app/controllers/likes_controller.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index 951deed..502eac6 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -11,7 +11,10 @@ def show; end # GET /likes/new def new - @like = Like.new + @post = Post.find(params[:id]) + @like = Like.new(author_id: params[:user_id], post_id: @post.id) + @like.save + redirect_to user_post_path end # GET /likes/1/edit From d6aea10ee86bff6c3e229f21e58f33af13378940 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 8 Jul 2023 23:47:41 +0300 Subject: [PATCH 13/16] add user instance to Posts_controller --- app/controllers/posts_controller.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 0414712..c103797 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -2,6 +2,7 @@ class PostsController < ApplicationController def index @user = User.find(params[:user_id]) @posts = @user.posts + @current_user = current_user end def show @@ -14,13 +15,6 @@ def new @post = Post.new end - def like - @post = Post.find(params[:id]) - @like = Like.new(author_id: params[:user_id], post_id: @post.id) - @like.save - redirect_to user_post_path - end - def create @post = Post.new(post_params) @post.author_id = current_user.id From 916afad0d5aeaf55a24232ac3983069647db3c4e Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 8 Jul 2023 23:48:40 +0300 Subject: [PATCH 14/16] add new posts form and button --- app/views/posts/index.html.erb | 3 +++ app/views/posts/new.html.erb | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index bf1376b..a23692c 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -58,6 +58,9 @@ <% end %> diff --git a/app/views/posts/new.html.erb b/app/views/posts/new.html.erb index 408a66e..9bebee6 100644 --- a/app/views/posts/new.html.erb +++ b/app/views/posts/new.html.erb @@ -2,11 +2,15 @@

Create New Post

<%= form_with(model: @post, url: user_posts_path, local: true) do |form| %> +
<%= form.label :title %> <%= form.text_field :title, name: 'post[title]' %> +
+
<%= form.label :text %> <%= form.text_area :text, name: 'post[text]' %> - <%= form.submit 'Create Post' %> +
+ <%= form.submit 'Create Post', class: 'btn-link' %> <% end %>
From ebae417291f0553849ce3b43aea70aca0bf7cb33 Mon Sep 17 00:00:00 2001 From: cherelemma Date: Sat, 8 Jul 2023 23:48:55 +0300 Subject: [PATCH 15/16] add style for new posts form --- app/assets/stylesheets/application.css | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 034caea..33fb974 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -104,10 +104,14 @@ a { align-self: center; } +.btn-link, .btn-link button { cursor: pointer; padding: 5px; font-size: 15px; +} + +button { border-bottom: 5px solid black; border-right: 3px solid black; border-radius: 5px; @@ -121,6 +125,7 @@ a { padding: 1rem; gap: 1rem; border: 3px solid black; + background-color: #f5f5f5; } .comments, @@ -142,3 +147,10 @@ a { border-right: 3px solid black; border-radius: 5px; } + +.form-elements { + display: flex; + flex-direction: row; + gap: 1rem; + margin: 1rem 0; +} \ No newline at end of file From f2a3d857bf490348b12cd67e8c56b818bcfc11ff Mon Sep 17 00:00:00 2001 From: Chere Lemma Date: Sat, 8 Jul 2023 23:58:28 +0300 Subject: [PATCH 16/16] Update application.css --- app/assets/stylesheets/application.css | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 33fb974..b46953e 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -104,6 +104,12 @@ a { align-self: center; } +button { + border-bottom: 5px solid black; + border-right: 3px solid black; + border-radius: 5px; +} + .btn-link, .btn-link button { cursor: pointer; @@ -111,12 +117,6 @@ a { font-size: 15px; } -button { - border-bottom: 5px solid black; - border-right: 3px solid black; - border-radius: 5px; -} - .post-form, .comment-form { width: 60%; @@ -153,4 +153,4 @@ button { flex-direction: row; gap: 1rem; margin: 1rem 0; -} \ No newline at end of file +}