From f92534936f987d156bb008db7b02a76868f65101 Mon Sep 17 00:00:00 2001 From: MosDevx Date: Thu, 18 May 2023 12:49:20 +0300 Subject: [PATCH 01/14] fix n+1 problem --- app/controllers/comments_controller.rb | 2 ++ app/controllers/posts_controller.rb | 15 +++++++++------ app/views/posts/_post.html.erb | 2 +- app/views/users/index.html.erb | 4 ++-- app/views/users/show.html.erb | 2 +- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 3fde61a..7c5aef0 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,8 +1,10 @@ class CommentsController < ApplicationController + def new @comment = Comment.new end + def create @user = User.find(params[:user_id]) @post = Post.find(params[:post_id]) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 0b74359..2b5101b 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -1,18 +1,21 @@ class PostsController < ApplicationController + + def index @user = User.find(params[:user_id]) - @posts = @user.posts - @tester = 'herer' + @posts =Post.includes(:user,comments: :user).where(user_id: params[:user_id]) + # @user = @posts.user + + + end def show - @post = Post.find(params[:id]) + @post = Post.includes(:comments).find(params[:id]) # @comments = @post.comments end - # def custom_post - # @post = Post.find(params[:id]) - # end + def new @post = Post.new diff --git a/app/views/posts/_post.html.erb b/app/views/posts/_post.html.erb index 1254022..7c5bad5 100644 --- a/app/views/posts/_post.html.erb +++ b/app/views/posts/_post.html.erb @@ -36,7 +36,7 @@ Post #<%= post.id%> by <%= post.user.name%> <%# show comments if specifically asked for by render %> <% if show_comments%> -<%= render partial:'/comments/comment' , collection:post.most_recent_comments %> +<%= render partial:'/comments/comment' , collection:post.comments %> <%end%> diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index d2257fa..17644fe 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -1,7 +1,7 @@ -

All Users

+

All Users

diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 27120dc..3b66ebb 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,4 +1,4 @@ - <%= render 'user', user:@user %> + <%= render @user %>
<%= link_to 'Edit User Data', edit_user_path %> From 8425dca6ce027087014bdd9c8aed4518942fce92 Mon Sep 17 00:00:00 2001 From: MosDevx Date: Thu, 18 May 2023 19:19:09 +0300 Subject: [PATCH 02/14] working on intergration --- app/controllers/users_controller.rb | 2 +- app/views/layouts/application.html.erb | 2 +- config/routes.rb | 2 +- spec/views/users/index.html.erb_spec.rb | 19 --------- spec/views/users/show_spec.rb | 51 +++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 22 deletions(-) create mode 100644 spec/views/users/show_spec.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 93a9ce5..176bda5 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,5 +1,5 @@ class UsersController < ApplicationController - before_action :authenticate_user! + # before_action :authenticate_user! def index @users = User.all diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index db495bd..86b7996 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -14,7 +14,7 @@
- <%if user_signed_in? %> + <%if false %>