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 @@
<%= render 'shared/show_user', user: @user %> - <% @user.posts.each do |post| %> +
+ <%= link_to "Create New Post", new_user_post_path(current_user.id), class: 'btn btn-primary' %> +
+ <% @user.posts.order(created_at: :asc).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/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/posts/show.html.erb b/app/views/posts/show.html.erb index ee63ffd..e77c68e 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -5,8 +5,10 @@

<%= @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 %> + <%= 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| %> +
+ <%= 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 %> 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/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
  • - - - 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 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 b99116a..259f3b0 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 do + resources :posts do + resources :comments + resources :likes + end + end end