-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from zunairkhan811/add-form
Add form to Blog App
- Loading branch information
Showing
18 changed files
with
123 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class HomesController < ApplicationController | ||
def index; end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |