Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add form to Blog App #7

Merged
merged 7 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,5 @@ group :test do
gem 'capybara'
gem 'selenium-webdriver'
end
gem 'bootstrap_form', '~> 5.4'
gem 'rubocop', '>= 1.0', '< 2.0'
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -262,6 +265,7 @@ PLATFORMS

DEPENDENCIES
bootsnap
bootstrap_form (~> 5.4)
capybara
debug
importmap-rails
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -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
zunairkhan811 marked this conversation as resolved.
Show resolved Hide resolved
zunairkhan811 marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 0 additions & 3 deletions app/controllers/home_controller.rb

This file was deleted.

3 changes: 3 additions & 0 deletions app/controllers/homes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class HomesController < ApplicationController
def index; end
end
20 changes: 20 additions & 0 deletions app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
@@ -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

zunairkhan811 marked this conversation as resolved.
Show resolved Hide resolved
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
11 changes: 10 additions & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

zunairkhan811 marked this conversation as resolved.
Show resolved Hide resolved
def show; end

private
Expand All @@ -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
9 changes: 7 additions & 2 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<div class="col-lg-12 mb-5">
<%= render 'shared/show_user', user: @user %>
<% @user.posts.each do |post| %>
<div class="mb-2 mt-2">
<%= link_to "Create New Post", new_user_post_path(current_user.id), class: 'btn btn-primary' %>
</div>
<% @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 %>

</div>

10 changes: 10 additions & 0 deletions app/views/posts/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div class="col-lg-12">
<div class="card mt-1 mb-5">
<div class="card-body">
<h2 class="text-center">Create New Post</h2>
</div>
</div>

<%= render 'shared/form', post: @post %>

</div>
4 changes: 3 additions & 1 deletion app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
<h3 class="font-weight-bold col-sm-7 mb-0"><%= @post.title %> By <%= @user.name %></h3>
<span class="card-text my-1 text-end col-sm-5">Comments: <%= @post.comments_counter %>, Likes: <%= @post.likes_counter %></span>
</div>
<p class="card-text mt-5"><%= @post.text %></p>
<p class="card-text mt-5"><%= @post.text %></p>
<%= render 'shared/like_form', like: @like, post: @post %>
</div>
</div>
<%= render 'shared/full_comment', post: @post %>
<%= render 'shared/comment_form', comment: @comment %>
</div>
6 changes: 6 additions & 0 deletions app/views/shared/_comment_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= form_with model: @comment, url: user_post_comments_path(current_user.id, @post) do |form| %>
<div class="form-group">
<%= 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" %>
</div>
<% end %>
26 changes: 26 additions & 0 deletions app/views/shared/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-5">
<%= form_with model: @post, url: user_posts_path do |form| %>
<div class="form-group">
<%= 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| %>
<p class="text-danger"><%= message %></p>
<% end %>
</div>
<div class="form-group">
<%= 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| %>
<p class="text-danger"><%= message %></p>
<% end %>
</div>
<div class="form-group text-center mt-3">
<%= form.submit "Create post", class: "btn btn-primary btn-sm" %>
</div>
<% end %>
</div>
</div>
</div>

3 changes: 0 additions & 3 deletions app/views/shared/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
<li><a class="dropdown-item" href="javascript:void(0)">Link 1</a></li>
</ul>
</li>



</ul>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions app/views/shared/_like_form.html.erb
Original file line number Diff line number Diff line change
@@ -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 %>
1 change: 1 addition & 0 deletions app/views/shared/_post.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<div class="card-body">
<h5><%= link_to "#{post.title}", user_post_path(@user, post.id), class: 'user-link' %></h5>
<p class="card-text"><%= post.text.slice(0,150).concat('...') %></p>
<%= render 'shared/like_form', like: @like, post: post %>
<p class="card-text text-end">Comments: <%= post.comments_counter %>, Likes: <%= post.likes_counter %></p>
</div>
</div>
3 changes: 3 additions & 0 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<div class="col-lg-12">
<%= render 'shared/show_user', user: @user %>
<div class="mb-2 mt-2">
<%= link_to "Create New Post", new_user_post_path(current_user.id), class: 'btn btn-primary' %>
</div>
<div class="card mb-3">
<h5 class="card-header">Bio</h5>
<div class="card-body">
Expand Down
18 changes: 10 additions & 8 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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
Loading