From 12ecb8b45072367e71e3d275ff0269abc9c2fcf2 Mon Sep 17 00:00:00 2001 From: Muhammad Zunair khan Date: Thu, 7 Dec 2023 23:42:23 +0500 Subject: [PATCH 1/7] add bootstrap-form gem --- Gemfile | 1 + Gemfile.lock | 4 ++++ app/assets/stylesheets/application.css | 1 + config/routes.rb | 18 ++++++++++-------- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index d57eb76..641a1c1 100644 --- a/Gemfile +++ b/Gemfile @@ -72,3 +72,4 @@ group :test do gem 'selenium-webdriver' end gem 'rubocop', '>= 1.0', '< 2.0' +gem "bootstrap_form", "~> 5.4" diff --git a/Gemfile.lock b/Gemfile.lock index 4d993a9..84e4dce 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -72,6 +72,9 @@ GEM bindex (0.8.1) bootsnap (1.17.0) msgpack (~> 1.2) + bootstrap_form (5.4.0) + actionpack (>= 6.1) + activemodel (>= 6.1) builder (3.2.4) capybara (3.39.2) addressable @@ -262,6 +265,7 @@ PLATFORMS DEPENDENCIES bootsnap + bootstrap_form (~> 5.4) capybara debug importmap-rails diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 288b9ab..d593009 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -12,4 +12,5 @@ * *= require_tree . *= require_self + *= require rails_bootstrap_forms */ diff --git a/config/routes.rb b/config/routes.rb index b99116a..53a0f56 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,11 +1,13 @@ Rails.application.routes.draw do - get 'users/:user_id/posts' => 'posts#index', as: 'user_posts' - get 'users/:user_id/posts/:id' => 'posts#show', as: 'user_post' - get 'users' => 'users#index', as: 'users' - get 'users/:id' => 'users#show', as: 'user' + # get 'users/:user_id/posts' => 'posts#index', as: 'user_posts' + # get 'users/:user_id/posts/:id' => 'posts#show', as: 'user_post' + # get 'users' => 'users#index', as: 'users' + # get 'users/:id' => 'users#show', as: 'user' root 'users#index' - # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html - - # Defines the root path route ("/") - # root "articles#index" + resources :users, only: [:index, :show] do + resources :posts do + resources :comments, only: [:new, :create] + resources :likes, only: [ :create, :new] + end + end end From 3edd9166b363681afc7d0503550a60ee9f9e14ce Mon Sep 17 00:00:00 2001 From: Muhammad Zunair khan Date: Fri, 8 Dec 2023 14:25:07 +0500 Subject: [PATCH 2/7] add post create form --- app/assets/stylesheets/application.css | 1 - app/controllers/application_controller.rb | 6 ++++++ app/controllers/posts_controller.rb | 9 ++++++++ app/controllers/users_controller.rb | 2 +- app/views/posts/index.html.erb | 3 +++ app/views/posts/new.html.erb | 10 +++++++++ app/views/shared/_form.html.erb | 26 +++++++++++++++++++++++ app/views/users/show.html.erb | 3 +++ config/routes.rb | 6 +++--- 9 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 app/views/posts/new.html.erb create mode 100644 app/views/shared/_form.html.erb diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index d593009..288b9ab 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -12,5 +12,4 @@ * *= require_tree . *= require_self - *= require rails_bootstrap_forms */ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d1..eede7e3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,8 @@ class ApplicationController < ActionController::Base + protect_from_forgery with: :exception + + def current_user + @current_user ||= User.find(params[:id] || params[:user_id]) if params[:id] || params[:user_id] + end + helper_method :current_user end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 2d08a7f..f300170 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -9,6 +9,15 @@ def new @post = Post.new end + def create + @post = @user.posts.new(post_params) + if @post.save + redirect_to user_posts_path(current_user), notice: "Your Post has been successfully Created" + else + render :new, notice: error + end + end + def show; end private diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 2c77723..d9bc579 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -8,7 +8,7 @@ def index def new @user = User.new end - + def show; end private diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 343e21f..03149b0 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -1,5 +1,8 @@
<%= render 'shared/show_user', user: @user %> +
+ <%= link_to "Create New Post", new_user_post_path(current_user.id), class: 'btn btn-primary' %> +
<% @user.posts.each do |post| %> <%= render 'shared/post', post: post %> <%= render 'shared/comment', post: post %> diff --git a/app/views/posts/new.html.erb b/app/views/posts/new.html.erb new file mode 100644 index 0000000..1e56077 --- /dev/null +++ b/app/views/posts/new.html.erb @@ -0,0 +1,10 @@ +
+
+
+

Create New Post

+
+
+ + <%= render 'shared/form', post: @post %> + +
diff --git a/app/views/shared/_form.html.erb b/app/views/shared/_form.html.erb new file mode 100644 index 0000000..4d59f2b --- /dev/null +++ b/app/views/shared/_form.html.erb @@ -0,0 +1,26 @@ +
+
+
+ <%= form_with model: @post, url: user_posts_path do |form| %> +
+ <%= form.label "#{:title}(*)", class: "lead mb-2" %> + <%= form.text_field :title, class: "form-control form-control-lg rounded-0" %> + <% @post.errors.full_messages_for(:title).each do |message| %> +

<%= message %>

+ <% end %> +
+
+ <%= form.label "#{:text}(*)", class: "lead mb-2" %> + <%= form.text_area :text, rows: 5, class: "form-control form-control-lg rounded-0" %> + <% @post.errors.full_messages_for(:text).each do |message| %> +

<%= message %>

+ <% end %> +
+
+ <%= form.submit "Create post", class: "btn btn-primary btn-sm" %> +
+ <% end %> +
+
+
+ \ No newline at end of file diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 3814ec3..610ee69 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,5 +1,8 @@
<%= render 'shared/show_user', user: @user %> +
+ <%= link_to "Create New Post", new_user_post_path(current_user.id), class: 'btn btn-primary' %> +
Bio
diff --git a/config/routes.rb b/config/routes.rb index 53a0f56..259f3b0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,10 +4,10 @@ # get 'users' => 'users#index', as: 'users' # get 'users/:id' => 'users#show', as: 'user' root 'users#index' - resources :users, only: [:index, :show] do + resources :users do resources :posts do - resources :comments, only: [:new, :create] - resources :likes, only: [ :create, :new] + resources :comments + resources :likes end end end From c225271acf720958ffde800bf99197e52a744ec7 Mon Sep 17 00:00:00 2001 From: Muhammad Zunair khan Date: Fri, 8 Dec 2023 21:59:03 +0500 Subject: [PATCH 3/7] add comment functionality to user posts --- app/controllers/application_controller.rb | 2 +- app/controllers/comments_controller.rb | 11 +++++++++++ app/controllers/posts_controller.rb | 2 +- app/views/posts/index.html.erb | 4 +++- app/views/posts/show.html.erb | 1 + app/views/shared/_comment_form.html.erb | 7 +++++++ 6 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 app/controllers/comments_controller.rb create mode 100644 app/views/shared/_comment_form.html.erb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index eede7e3..470db00 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,7 +2,7 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception def current_user - @current_user ||= User.find(params[:id] || params[:user_id]) if params[:id] || params[:user_id] + @current_user ||= User.find(params[:id]|| params[:user_id] ) if params[:id] || params[:user_id] end helper_method :current_user end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..7564b8a --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,11 @@ +class CommentsController < ApplicationController + def create + @comment = Comment.new(user_id: current_user.id, post_id: params[:post_id], text: params[:text]) + if @comment.save + redirect_to user_post_path(:user_id, :post_id), notice: "Your comment has been successfully created." + else + redirect_to user_post_path(:user_id, :post_id), alert: "Error creating comment." + end + end +end + \ No newline at end of file diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index f300170..b2658f9 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -33,6 +33,6 @@ def set_user def set_post @post = @user.posts.find(params[:id]) rescue ActiveRecord::RecordNotFound => e - redirect_to posts_path, notice: e + redirect_to user_post_path, notice: e end end diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index 03149b0..dba72d3 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -5,7 +5,9 @@
<% @user.posts.each do |post| %> <%= render 'shared/post', post: post %> - <%= render 'shared/comment', post: post %> + <%= render 'shared/comment', post: post %> + <%= link_to 'Add Comment', user_post_path(@user, post.id) ,class: " mt-1 col-lg-2 btn btn-primary btn-outline-warning border-primary" %> <% end %> +
diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index ee63ffd..05791a4 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -9,4 +9,5 @@
<%= render 'shared/full_comment', post: @post %> + <%= render 'shared/comment_form', comment: @comment %> diff --git a/app/views/shared/_comment_form.html.erb b/app/views/shared/_comment_form.html.erb new file mode 100644 index 0000000..1ea08b1 --- /dev/null +++ b/app/views/shared/_comment_form.html.erb @@ -0,0 +1,7 @@ + +<%= form_with model: @comment, url: user_post_comments_path(current_user.id, @post) do |form| %> +
+ <%= form.text_field :text, class: "form-control form-control-lg col-lg-9", placeholder: "Add a comment..." %> + <%= form.submit 'Add Comment', class: "mt-1 col-lg-3 btn btn-primary" %> +
+<% end %> From 01e6f5fc1a7a93e26ffdf0b523f0d81efeb3b3ce Mon Sep 17 00:00:00 2001 From: Muhammad Zunair khan Date: Fri, 8 Dec 2023 23:13:36 +0500 Subject: [PATCH 4/7] add like functionality to posts by creating a like button --- app/controllers/home_controller.rb | 3 --- app/controllers/homes_controller.rb | 3 +++ app/controllers/likes_controller.rb | 23 +++++++++++++++++++++++ app/views/posts/index.html.erb | 2 +- app/views/posts/show.html.erb | 3 ++- app/views/shared/_comment_form.html.erb | 1 - app/views/shared/_like_form.html.erb | 3 +++ app/views/shared/_post.html.erb | 1 + 8 files changed, 33 insertions(+), 6 deletions(-) delete mode 100644 app/controllers/home_controller.rb create mode 100644 app/controllers/homes_controller.rb create mode 100644 app/controllers/likes_controller.rb create mode 100644 app/views/shared/_like_form.html.erb diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb deleted file mode 100644 index 6d3fe90..0000000 --- a/app/controllers/home_controller.rb +++ /dev/null @@ -1,3 +0,0 @@ -class HomeController < ApplicationController - def index; end -end diff --git a/app/controllers/homes_controller.rb b/app/controllers/homes_controller.rb new file mode 100644 index 0000000..18aaaa7 --- /dev/null +++ b/app/controllers/homes_controller.rb @@ -0,0 +1,3 @@ +class HomesController < ApplicationController + def index; end +end diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb new file mode 100644 index 0000000..55326b5 --- /dev/null +++ b/app/controllers/likes_controller.rb @@ -0,0 +1,23 @@ +class LikesController < ApplicationController + def create + @like = Like.new(user_id: params[:user_id], post_id: params[:post_id]) + if @like.save + redirect_to redirect_url, notice: "You have successfully liked the post" + else + redirect_to redirect_url, alert: "Error occurred while liking the post" + end + end + + private + + def redirect_url + if request.referer.present? && request.referer.include?("/users/#{params[:user_id]}/posts") + # If the referrer URL contains "/users/:user_id/posts", redirect to that URL + request.referer + else + # Default redirect URL if no referrer or referrer doesn't match the pattern + user_posts_path(params[:user_id]) + end + 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 dba72d3..b905a9e 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -3,7 +3,7 @@
<%= link_to "Create New Post", new_user_post_path(current_user.id), class: 'btn btn-primary' %>
- <% @user.posts.each do |post| %> + <% @user.posts.order(created_at: :asc).each do |post| %> <%= render 'shared/post', post: post %> <%= render 'shared/comment', post: post %> <%= link_to 'Add Comment', user_post_path(@user, post.id) ,class: " mt-1 col-lg-2 btn btn-primary btn-outline-warning border-primary" %> diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 05791a4..e77c68e 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -5,7 +5,8 @@

<%= @post.title %> By <%= @user.name %>

Comments: <%= @post.comments_counter %>, Likes: <%= @post.likes_counter %> -

<%= @post.text %>

+

<%= @post.text %>

+ <%= render 'shared/like_form', like: @like, post: @post %> <%= render 'shared/full_comment', post: @post %> diff --git a/app/views/shared/_comment_form.html.erb b/app/views/shared/_comment_form.html.erb index 1ea08b1..fada439 100644 --- a/app/views/shared/_comment_form.html.erb +++ b/app/views/shared/_comment_form.html.erb @@ -1,4 +1,3 @@ - <%= form_with model: @comment, url: user_post_comments_path(current_user.id, @post) do |form| %>
<%= form.text_field :text, class: "form-control form-control-lg col-lg-9", placeholder: "Add a comment..." %> diff --git a/app/views/shared/_like_form.html.erb b/app/views/shared/_like_form.html.erb new file mode 100644 index 0000000..f9aad5d --- /dev/null +++ b/app/views/shared/_like_form.html.erb @@ -0,0 +1,3 @@ +<%= form_with model: like, url: user_post_likes_path(user_id: @user.id, post_id: post.id) do |form| %> + <%= form.submit 'Like', class: "mt-1 col-lg-1 btn btn-outline-dark" %> +<% end %> \ No newline at end of file diff --git a/app/views/shared/_post.html.erb b/app/views/shared/_post.html.erb index 1220e0e..12f9e49 100644 --- a/app/views/shared/_post.html.erb +++ b/app/views/shared/_post.html.erb @@ -2,6 +2,7 @@
<%= link_to "#{post.title}", user_post_path(@user, post.id), class: 'user-link' %>

<%= post.text.slice(0,150).concat('...') %>

+ <%= render 'shared/like_form', like: @like, post: post %>

Comments: <%= post.comments_counter %>, Likes: <%= post.likes_counter %>

\ No newline at end of file From d269d87c1bfb90923fdfc0fc1235e2be9075eb1b Mon Sep 17 00:00:00 2001 From: Muhammad Zunair khan Date: Sat, 9 Dec 2023 00:39:30 +0500 Subject: [PATCH 5/7] fix current user issue --- app/controllers/application_controller.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 470db00..23c72ed 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,8 +1,8 @@ class ApplicationController < ActionController::Base - protect_from_forgery with: :exception + protect_from_forgery with: :exception + helper_method :current_user def current_user - @current_user ||= User.find(params[:id]|| params[:user_id] ) if params[:id] || params[:user_id] + @current_user ||= User.first end - helper_method :current_user end From 56014b37aa23dfec0ff4e400327590467de6cd77 Mon Sep 17 00:00:00 2001 From: Muhammad Zunair khan Date: Sat, 9 Dec 2023 00:42:00 +0500 Subject: [PATCH 6/7] fix linter errors --- Gemfile | 2 +- app/controllers/comments_controller.rb | 15 +++++------ app/controllers/likes_controller.rb | 37 +++++++++++++------------- app/controllers/posts_controller.rb | 2 +- app/controllers/users_controller.rb | 2 +- 5 files changed, 28 insertions(+), 30 deletions(-) diff --git a/Gemfile b/Gemfile index 641a1c1..bb3add1 100644 --- a/Gemfile +++ b/Gemfile @@ -71,5 +71,5 @@ group :test do gem 'capybara' gem 'selenium-webdriver' end +gem 'bootstrap_form', '~> 5.4' gem 'rubocop', '>= 1.0', '< 2.0' -gem "bootstrap_form", "~> 5.4" diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 7564b8a..631825f 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,11 +1,10 @@ class CommentsController < ApplicationController - def create - @comment = Comment.new(user_id: current_user.id, post_id: params[:post_id], text: params[:text]) - if @comment.save - redirect_to user_post_path(:user_id, :post_id), notice: "Your comment has been successfully created." - else - redirect_to user_post_path(:user_id, :post_id), alert: "Error creating comment." - end + def create + @comment = Comment.new(user_id: current_user.id, post_id: params[:post_id], text: params[:text]) + if @comment.save + redirect_to user_post_path(:user_id, :post_id), notice: 'Your comment has been successfully created.' + else + redirect_to user_post_path(:user_id, :post_id), alert: 'Error creating comment.' end + end end - \ No newline at end of file diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index 55326b5..1de199d 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -1,23 +1,22 @@ class LikesController < ApplicationController - def create - @like = Like.new(user_id: params[:user_id], post_id: params[:post_id]) - if @like.save - redirect_to redirect_url, notice: "You have successfully liked the post" - else - redirect_to redirect_url, alert: "Error occurred while liking the post" - end + def create + @like = Like.new(user_id: params[:user_id], post_id: params[:post_id]) + if @like.save + redirect_to redirect_url, notice: 'You have successfully liked the post' + else + redirect_to redirect_url, alert: 'Error occurred while liking the post' end - - private - - def redirect_url - if request.referer.present? && request.referer.include?("/users/#{params[:user_id]}/posts") - # If the referrer URL contains "/users/:user_id/posts", redirect to that URL - request.referer - else - # Default redirect URL if no referrer or referrer doesn't match the pattern - user_posts_path(params[:user_id]) - end + end + + private + + def redirect_url + if request.referer.present? && request.referer.include?("/users/#{params[:user_id]}/posts") + # If the referrer URL contains "/users/:user_id/posts", redirect to that URL + request.referer + else + # Default redirect URL if no referrer or referrer doesn't match the pattern + user_posts_path(params[:user_id]) end + end end - \ No newline at end of file diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index b2658f9..aba685f 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -12,7 +12,7 @@ def new def create @post = @user.posts.new(post_params) if @post.save - redirect_to user_posts_path(current_user), notice: "Your Post has been successfully Created" + redirect_to user_posts_path(current_user), notice: 'Your Post has been successfully Created' else render :new, notice: error end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d9bc579..2c77723 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -8,7 +8,7 @@ def index def new @user = User.new end - + def show; end private From 9e1da857b244f0e6237096412eb5488ee195d85d Mon Sep 17 00:00:00 2001 From: Muhammad Zunair khan Date: Sat, 9 Dec 2023 00:45:17 +0500 Subject: [PATCH 7/7] fix spacing and linter errors --- app/controllers/likes_controller.rb | 2 -- app/views/shared/_header.html.erb | 3 --- 2 files changed, 5 deletions(-) diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index 1de199d..79acdd2 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -12,10 +12,8 @@ def create def redirect_url if request.referer.present? && request.referer.include?("/users/#{params[:user_id]}/posts") - # If the referrer URL contains "/users/:user_id/posts", redirect to that URL request.referer else - # Default redirect URL if no referrer or referrer doesn't match the pattern user_posts_path(params[:user_id]) end end diff --git a/app/views/shared/_header.html.erb b/app/views/shared/_header.html.erb index cbece61..29a2baa 100644 --- a/app/views/shared/_header.html.erb +++ b/app/views/shared/_header.html.erb @@ -15,9 +15,6 @@
  • Link 1
  • - - -