-
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 #6 from Microverse-Fullstack-Program/add-forms
[Blog app] Milestone 6 - Add Forms
- Loading branch information
Showing
11 changed files
with
218 additions
and
8 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
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 |
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,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 |
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,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 |
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,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> |
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 |
---|---|---|
@@ -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> | ||
``` | ||
|
||
### |
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