diff --git a/Gemfile b/Gemfile index d57eb76..bb3add1 100644 --- a/Gemfile +++ b/Gemfile @@ -71,4 +71,5 @@ group :test do gem 'capybara' gem 'selenium-webdriver' end +gem 'bootstrap_form', '~> 5.4' gem 'rubocop', '>= 1.0', '< 2.0' 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/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d1..23c72ed 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 + helper_method :current_user + + def current_user + @current_user ||= User.first + end end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..631825f --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +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 + end +end 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..79acdd2 --- /dev/null +++ b/app/controllers/likes_controller.rb @@ -0,0 +1,20 @@ +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") + request.referer + else + user_posts_path(params[:user_id]) + end + end +end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 2d08a7f..aba685f 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 @@ -24,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 343e21f..b905a9e 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -1,8 +1,13 @@
<%= @post.text %>
+<%= @post.text %>
+ <%= render 'shared/like_form', like: @like, post: @post %> <%= 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..fada439 --- /dev/null +++ b/app/views/shared/_comment_form.html.erb @@ -0,0 +1,6 @@ +<%= form_with model: @comment, url: user_post_comments_path(current_user.id, @post) do |form| %> +<%= message %>
+ <% end %> +<%= message %>
+ <% end %> +<%= post.text.slice(0,150).concat('...') %>
+ <%= render 'shared/like_form', like: @like, post: post %>Comments: <%= post.comments_counter %>, Likes: <%= post.likes_counter %>