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 forms #9

Merged
merged 13 commits into from
Aug 24, 2023
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- [📖 About the Project](#about-project)
- [🛠 Built With](#built-with)
- [Key Features](#key-features)
- [🚀 Live Demo](#live-demo)
<!-- - [🚀 Live Demo](#live-demo) -->
- [💻 Getting Started](#getting-started)
- [Prerequisites](#prerequisites)
- [Setup](#setup)
Expand Down Expand Up @@ -51,6 +51,8 @@
- [x] **Display Likes**
- [x] **Validation & Unit Tests**
- [x] **Controllers**
- [x] **Views**
- [x] **Add Forms**

<p align="right">(<a href="#readme-top">back to top</a>)</p>

Expand Down Expand Up @@ -109,7 +111,7 @@ bundle exec rspec spec/models

Nasirkhan294 marked this conversation as resolved.
Show resolved Hide resolved
Nasirkhan294 marked this conversation as resolved.
Show resolved Hide resolved
## 🔭 Future Features <a name="future-features"></a>

- [ ] **Add forms**
- [ ] **Add devise**

<p align="right">(<a href="#readme-top">back to top</a>)</p>

Expand Down
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
User.first
end
end
26 changes: 26 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class CommentsController < ApplicationController
def new
@comment = Comment.new
@post = Post.find(params[:post_id])
end

def create
comment = Comment.new(comment_params)
comment.post = Post.find(params[:post_id])
comment.author = current_user

if comment.save
flash[:success] = 'Comment saved successfully'
redirect_to '/'
else
flash.now[:error] = 'error: comment could not be saved'
redirect_to new_comment
end
end

private

def comment_params
params.require(:comment).permit(:text)
end
end
15 changes: 15 additions & 0 deletions app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class LikesController < ApplicationController
def create
like = Like.new
like.post = Post.find(params[:post_id])
like.author = current_user

if like.save
flash[:success] = 'Like saved successfully'
else
flash.now[:error] = 'Error: Like could not be saved'
end

redirect_to '/'
end
end
30 changes: 29 additions & 1 deletion app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class PostsController < ActionController::Base
class PostsController < ApplicationController
def index
page = params[:page] || 1
per_page = 10
Expand All @@ -16,5 +16,33 @@ def index

def show
@post = Post.find(params[:id])
@current_user = current_user
@like = Like.new
end
Nasirkhan294 marked this conversation as resolved.
Show resolved Hide resolved

def create
post = Post.new(post_params)
post.author = current_user
post.comments_counter = 0
post.likes_counter = 0

if post.save
flash[:success] = 'post saved successfully'
redirect_to '/'
else
flash.now[:error] = 'error: question could not be saved'
redirect_to new_user_post_path
end
end

Nasirkhan294 marked this conversation as resolved.
Show resolved Hide resolved
def new
@post = Post.new
@current_user = current_user
end

private

def post_params
params.require(:post).permit(:title, :text)
end
end
3 changes: 2 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class UsersController < ActionController::Base
class UsersController < ApplicationController
def index
@users = User.all
@current_user = current_user
end

def show
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/comments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CommentsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/likes_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module LikesHelper
end
21 changes: 21 additions & 0 deletions app/views/comments/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<div class="container mt-5">
<h1>Add New Comment to post: <%= @post.title %></h1>
<% if @comment.errors.any? %>
<div class="post-errors-box">
<p><%= pluralize(@comment.errors.count, "error") %> found:</p>

<ul class="post-errors-ul">
<% @comment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_with model: @comment do |form| %>
<div class="form-group">
<%= form.label :text %>
<%= form.text_area :text, size: "70x5", class: 'form-control' %>
</div>
<%= form.submit class: 'btn btn-primary mt-3' %>
<% end %>
</div>
2 changes: 2 additions & 0 deletions app/views/likes/create.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Likes#create</h1>
<p>Find me in app/views/likes/create.html.erb</p>
1 change: 1 addition & 0 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="container mt-5">
<%= render 'shared/user_card', user: @author %>

<% if @posts.empty? %>
<p>This user has no posts yet</p>
<% end %>
Expand Down
25 changes: 25 additions & 0 deletions app/views/posts/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div class="container mt-5">
<h1>Add New Post</h1>
<% if @post.errors.any? %>
<div class="post-errors-box">
<p><%= pluralize(@post.errors.count, "error") %> found:</p>

<ul class="post-errors-ul">
<% @post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= form_with url: "/users/#{@current_user.id}/posts", model: @post do |form| %>
<div class="form-group">
<%= form.label :title %>
<%= form.text_field :title, class: 'form-control' %>
</div>
<div class="form-group">
<%= form.label :text %>
<%= form.text_area :text, size: "70x5", class: 'form-control' %>
</div>
<%= form.submit class: 'btn btn-primary mt-3' %>
<% end %>
</div>
4 changes: 4 additions & 0 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
</div>
<p class="mt-2"><%= @post.text %></p>
</div>
<div class="card-footer">
<%= render 'shared/like' %>
</div>
</div>
<%= render 'shared/comments_card', comments: @post.comments unless @post.comments.empty? %>
<%= link_to 'Add a new comment', "/users/#{@current_user.id}/posts/#{@post.id}/comments/new", class: "btn btn-primary mt-3" %>
</div>
3 changes: 3 additions & 0 deletions app/views/shared/_like.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%= form_with url: "/users/#{@current_user.id}/posts/#{@post.id}/likes", model: @like do |form| %>
<%= form.submit "Like", class: 'btn btn-primary' %>
<% end %>
1 change: 1 addition & 0 deletions app/views/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<%= render 'shared/user_card', user: user %>
</a>
<% end %>
<%= link_to 'Add a new post', "/users/#{@current_user.id}/posts/new", class: "btn btn-primary" %>
</div>
7 changes: 6 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Rails.application.routes.draw do
root 'user#index'
resources :users, only: %i[index show] do
resources :posts, only: %i[index show]
resources :posts, only: %i[index show create new]
end

get 'users/:user_id/posts/:post_id/comments/new', to: 'comments#new'
post 'users/:user_id/posts/:post_id/comments', to: 'comments#create', as: 'comments'
post 'users/:user_id/posts/:post_id/likes', to: 'likes#create', as: 'likes'
end
10 changes: 8 additions & 2 deletions spec/requests/posts_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
require 'rails_helper'
RSpec.describe 'Posts', type: :request do
before(:all) do
@user = User.create(name: 'Tom', photo: 'https://placehold.co/200x133', bio: 'Teacher from Mexico.',
posts_counter: 0)
@post = Post.create(author: @user, title: 'Hello', text: 'This is my first post', comments_counter: 0,
likes_counter: 0)
end
describe 'GET /index' do
it 'renders the posts index template and includes correct placeholder' do
User.create(name: 'Nasir')
get '/users/1/posts'
get "/users/#{@user.id}/posts"
expect(response).to be_successful
expect(response).to render_template(:index)
expect(response.body).to include('Here is a list of posts for a given user')
Expand All @@ -12,7 +18,7 @@
describe 'GET /show' do
it 'renders the post show template and includes correct placeholder' do
Post.create(title: 'My Post')
get '/users/1/posts/1'
get "/users/#{@user.id}/posts/#{@post.id}"
expect(response).to be_successful
expect(response).to render_template(:show)
expect(response.body).to include('Here are details for a given post')
Expand Down
8 changes: 5 additions & 3 deletions spec/requests/users_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
require 'rails_helper'
RSpec.describe 'Users', type: :request do
before(:all) do
@user = User.create(name: 'Tom', photo: 'https://placehold.co/200x133', bio: 'Teacher from Mexico.',
posts_counter: 0)
end
describe 'GET /index' do
it 'renders the users index template and includes correct placeholder' do
get '/users'
expect(response).to be_successful
expect(response).to render_template(:index)
expect(response.body).to include('Here is a the list of all users')
end
end
describe 'GET /show' do
it 'renders the users show template and includes correct placeholder' do
User.create(name: 'Nasir')
get '/users/1'
get "/users/#{@user.id}"
expect(response).to be_successful
expect(response).to render_template(:show)
expect(response.body).to include('Here are details for a given user')
end
end
end
Loading