Skip to content

Commit

Permalink
Merge pull request #6 from Microverse-Fullstack-Program/add-forms
Browse files Browse the repository at this point in the history
[Blog app] Milestone 6 - Add Forms
  • Loading branch information
cherelemma authored Jul 8, 2023
2 parents cf0c741 + f2a3d85 commit ca11c88
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 8 deletions.
42 changes: 42 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,53 @@ a {
align-self: center;
}

button {
border-bottom: 5px solid black;
border-right: 3px solid black;
border-radius: 5px;
}

.btn-link,
.btn-link button {
cursor: pointer;
padding: 5px;
font-size: 15px;
}

.post-form,
.comment-form {
width: 60%;
display: flex;
flex-direction: column;
padding: 1rem;
gap: 1rem;
border: 3px solid black;
background-color: #f5f5f5;
}

.comments,
.posts {
width: 80%;
display: flex;
flex-direction: column;
padding: 1rem;
gap: 1rem;
}

.comment-btn,
.like-button {
cursor: pointer;
padding: 5px 10px;
margin-left: 5rem;
font-size: 15px;
border-bottom: 5px solid black;
border-right: 3px solid black;
border-radius: 5px;
}

.form-elements {
display: flex;
flex-direction: row;
gap: 1rem;
margin: 1rem 0;
}
3 changes: 3 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
class ApplicationController < ActionController::Base
def current_user
@current_user ||= User.first
end
end
25 changes: 23 additions & 2 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
class CommentsController < ApplicationController
def index; end
def new
@comment = Comment.new
@user = User.find(params[:user_id])
@post = Post.find(params[:post_id])
end

def show; end
def create
@comment = Comment.new(comment_params)
@comment.user_id = current_user.id
@comment.post_id = params[:post_id]

if @comment.save
redirect_to user_post_path(user_id: @comment.user_id, id: @comment.post_id),
notice: 'Comment was successfully created.'
else
render 'new'
end
end

private

def comment_params
params.require(:comment).permit(:content, :text)
end
end
69 changes: 68 additions & 1 deletion app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,72 @@
class LikesController < ApplicationController
def index; end
before_action :set_like, only: %i[show edit update destroy]

# GET /likes or /likes.json
def index
@likes = Like.all
end

# GET /likes/1 or /likes/1.json
def show; end

# GET /likes/new
def new
@post = Post.find(params[:id])
@like = Like.new(author_id: params[:user_id], post_id: @post.id)
@like.save
redirect_to user_post_path
end

# GET /likes/1/edit
def edit; end

# POST /likes or /likes.json
def create
@like = Like.new(like_params)

respond_to do |format|
if @like.save
format.html { redirect_to like_url(@like), notice: 'Like was successfully created.' }
format.json { render :show, status: :created, location: @like }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @like.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /likes/1 or /likes/1.json
def update
respond_to do |format|
if @like.update(like_params)
format.html { redirect_to like_url(@like), notice: 'Like was successfully updated.' }
format.json { render :show, status: :ok, location: @like }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @like.errors, status: :unprocessable_entity }
end
end
end

# DELETE /likes/1 or /likes/1.json
def destroy
@like.destroy

respond_to do |format|
format.html { redirect_to likes_url, notice: 'Like was successfully destroyed.' }
format.json { head :no_content }
end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_like
@like = Like.find(params[:id])
end

# Only allow a list of trusted parameters through.
def like_params
params.require(:like).permit(:AuthorId, :PostId, :UpdatedAt, :CreatedAt)
end
end
24 changes: 24 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,35 @@ class PostsController < ApplicationController
def index
@user = User.find(params[:user_id])
@posts = @user.posts
@current_user = current_user
end

def show
@user = User.find(params[:user_id])
@post = @user.posts.find(params[:id])
@comments = @post.comments
end

def new
@post = Post.new
end

def create
@post = Post.new(post_params)
@post.author_id = current_user.id
@post.comments_counter = 0
@post.likes_counter = 0

if @post.save
redirect_to user_posts_path, notice: 'Post was successfully created.'
else
render 'new'
end
end

private

def post_params
params.require(:post).permit(:title, :text)
end
end
13 changes: 13 additions & 0 deletions app/views/comments/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<section class="container">
<div class="comment-form">
<h3>Add Comment</h3>
<%= form_with(model: @comment, url: user_post_comments_url(@post.author, @post), local: true) do |form| %>
<div class="comments">
<%= form.text_area :text %>
</div>
<div>
<%= form.submit 'Create Comment', class: 'comment-btn'%>
</div>
<% end %>
</div>
</section>
2 changes: 1 addition & 1 deletion app/views/layouts/mailer.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
/* Email styles need to be inline */
</style>
Expand Down
9 changes: 6 additions & 3 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

<section class="container">
<div class="user">
<div class="user-photo">
<img src="<%= @user.photo %>" alt="User Photo">
<div class="user">
<div class="user-photo">
<img src="<%= @user.photo %>" alt="User Photo">
</div>

<div class="user-info">
Expand Down Expand Up @@ -58,6 +58,9 @@
<% end %>

<div class="btn-link">
<button>
<%= link_to 'Add New Post', new_user_post_path(@user) %>
</button>
<button>Pagination</button>
</div>
</section>
19 changes: 19 additions & 0 deletions app/views/posts/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<section class="container">
<div class = "post-form">
<h3>Create New Post</h3>
<%= form_with(model: @post, url: user_posts_path, local: true) do |form| %>
<div class="form-elements">
<%= form.label :title %>
<%= form.text_field :title, name: 'post[title]' %>
</div>
<div class="form-elements">
<%= form.label :text %>
<%= form.text_area :text, name: 'post[text]' %>
</div>
<%= form.submit 'Create Post', class: 'btn-link' %>
<% end %>
</div>
</section>
```

###
15 changes: 15 additions & 0 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
</span>
</div>
<p><%= @post.text %></p>
<div>
<% if @current_user %>
<%= form_with(model: @post, url: like_user_post_path(user_id: @current_user.id, id: @post.id), method: :post, local: true) do |form| %>
<%= form.button type: :submit, class: 'like-button' do %>
Like
<% end %>
<% end %>
<% end %>
</div>
</div>

<div class="post-comments">
Expand All @@ -31,5 +40,11 @@
</div>
<% end %>
</div>

<div class ="like-comment">
<%= link_to new_user_post_comment_url(@user, @post), class: 'btn-link' do %>
<button>Add comment</button>
<% end %>
</div>
</section>

5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
root "users#index"

resources :users, only: [:index, :show] do
resources :posts, only: [:index, :show]
resources :posts, only: [:index, :show, :new, :create] do
resources :comments, only: [:new, :create]
post 'like', on: :member
end
end
end

0 comments on commit ca11c88

Please sign in to comment.