Created
May 3, 2020 03:20
-
-
Save anon987654321/2eacd49873fbdb471ac6b190800c8972 to your computer and use it in GitHub Desktop.
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
commit c34330afa6646893f058e54ddbe29fa9118046e6 | |
Author: dev <dev@dev.my.domain> | |
Date: Wed Jan 1 00:00:00 2020 -0100 | |
Add comments to posts | |
diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb | |
new file mode 100644 | |
index 0000000..40e773d | |
--- /dev/null | |
+++ b/app/controllers/comments_controller.rb | |
@@ -0,0 +1,46 @@ | |
+class CommentsController < ApplicationController | |
+ before_action :authenticate_user! | |
+ #resources :comments, except: [:index, :show] | |
+ | |
+ def create | |
+ @post = Post.find(params[:post_id]) | |
+ @comment = Comment.new(comment_params) | |
+ @comment.post_id = params[:post_id] | |
+ @comment.user = current_user | |
+ @comment.display_name = current_user.display_name | |
+ | |
+ @comment.save | |
+ | |
+ redirect_to post_path(@post), notice: "Comment saved." | |
+ end | |
+ | |
+ def edit | |
+ @post = Post.find(params[:post_id]) | |
+ @comment = @post.comments.find(params[:id]) | |
+ end | |
+ | |
+ def update | |
+ @post = Post.find(params[:post_id]) | |
+ @comment = @post.comments.find(params[:id]) | |
+ | |
+ if @comment.update(comment_params) | |
+ redirect_to post_path(@post), notice: "Comment updated." | |
+ else | |
+ render "edit" | |
+ end | |
+ end | |
+ | |
+ def destroy | |
+ @post = Post.find(params[:post_id]) | |
+ @comment = @post.comments.find(params[:id]) | |
+ | |
+ @comment.destroy | |
+ | |
+ redirect_to post_path(@post), notice: "Comment deleted." | |
+ end | |
+ | |
+ private | |
+ def comment_params | |
+ params.require(:comment).permit(:body) | |
+ end | |
+end | |
diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb | |
index 3e29570..3bb6e58 100644 | |
--- a/app/controllers/posts_controller.rb | |
+++ b/app/controllers/posts_controller.rb | |
@@ -2,64 +2,49 @@ class PostsController < ApplicationController | |
before_action :set_post, only: [:show, :edit, :update, :destroy] | |
before_action :authenticate_user!, except: [:index, :show] | |
- # GET /posts | |
- # GET /posts.json | |
def index | |
@posts = Post.all | |
end | |
- # GET /posts/1 | |
- # GET /posts/1.json | |
def show | |
+ @comment = Comment.new | |
+ @comment.post_id = @post.id | |
end | |
- # GET /posts/new | |
def new | |
@post = current_user.posts.build | |
end | |
- # GET /posts/1/edit | |
- def edit | |
- end | |
- | |
- # POST /posts | |
- # POST /posts.json | |
def create | |
@post = current_user.posts.build(post_params) | |
- respond_to do |format| | |
- if @post.save | |
- format.html { redirect_to @post, notice: 'Post was successfully created.' } | |
- format.json { render :show, status: :created, location: @post } | |
- else | |
- format.html { render :new } | |
- format.json { render json: @post.errors, status: :unprocessable_entity } | |
- end | |
+ if @post.save | |
+ redirect_to @post, notice: "Post created." | |
+ else | |
+ render :new | |
+ end | |
+ end | |
+ | |
+ def edit | |
+ unless current_user == @post.user | |
+ redirect_to(@post, notice: "You cannot edit this post.") | |
end | |
end | |
- # PATCH/PUT /posts/1 | |
- # PATCH/PUT /posts/1.json | |
def update | |
- respond_to do |format| | |
- if @post.update(post_params) | |
- format.html { redirect_to @post, notice: 'Post was successfully updated.' } | |
- format.json { render :show, status: :ok, location: @post } | |
- else | |
- format.html { render :edit } | |
- format.json { render json: @post.errors, status: :unprocessable_entity } | |
- end | |
+ if @post.update(post_params) | |
+ redirect_to post_path(@post), notice: "Post updated." | |
+ else | |
+ render 'edit' | |
end | |
end | |
- # DELETE /posts/1 | |
- # DELETE /posts/1.json | |
def destroy | |
+ @post = Post.find(params[:id]) | |
+ | |
@post.destroy | |
- respond_to do |format| | |
- format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' } | |
- format.json { head :no_content } | |
- end | |
+ | |
+ redirect_to root_path, notice: "Post destroyed." | |
end | |
private | |
diff --git a/app/models/comment.rb b/app/models/comment.rb | |
new file mode 100644 | |
index 0000000..4a019df | |
--- /dev/null | |
+++ b/app/models/comment.rb | |
@@ -0,0 +1,4 @@ | |
+class Comment < ApplicationRecord | |
+ belongs_to :post | |
+ belongs_to :user | |
+end | |
diff --git a/app/models/post.rb b/app/models/post.rb | |
index a82b7d2..f1fd8bc 100644 | |
--- a/app/models/post.rb | |
+++ b/app/models/post.rb | |
@@ -3,5 +3,8 @@ class Post < ApplicationRecord | |
belongs_to :user | |
+ # When destroying a post, destroy its comments too | |
+ has_many :comments, dependent: :destroy | |
end | |
diff --git a/app/models/user.rb b/app/models/user.rb | |
index cc7376a..3221c60 100644 | |
--- a/app/models/user.rb | |
+++ b/app/models/user.rb | |
@@ -4,6 +4,7 @@ class User < ApplicationRecord | |
after_create :set_admin | |
has_many :posts | |
+ has_many :comments | |
acts_as_textcaptcha | |
diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb | |
new file mode 100644 | |
index 0000000..d4d5efe | |
--- /dev/null | |
+++ b/app/views/comments/_form.html.erb | |
@@ -0,0 +1,10 @@ | |
+<%= form_for [ @post, @comment ] do |form| %> | |
+ <div class="field"> | |
+ <%= form.label :body %> | |
+ <%= form.text_area :body %> | |
+ </div> | |
+ <div class="action"> | |
+ <%= form.submit "Submit" %> | |
+ </div> | |
+<% end %> | |
+ | |
diff --git a/app/views/comments/edit.html.erb b/app/views/comments/edit.html.erb | |
new file mode 100644 | |
index 0000000..844bdfa | |
--- /dev/null | |
+++ b/app/views/comments/edit.html.erb | |
@@ -0,0 +1,4 @@ | |
+<h1>Edit Comment</h1> | |
+<%= render "form", comment: @comment %> | |
+<%= link_to "Back", posts_path %> | |
diff --git a/app/views/posts/_comment.html.erb b/app/views/posts/_comment.html.erb | |
new file mode 100755 | |
index 0000000..ef8ada1 | |
--- /dev/null | |
+++ b/app/views/posts/_comment.html.erb | |
@@ -0,0 +1,11 @@ | |
+<div> | |
+ <h4><%= comment.display_name %></h4> | |
+ <p><%= comment.body %></p> | |
+ <% if user_signed_in? && current_user == comment.user %> | |
+ <%= link_to "Edit", edit_post_comment_path(@post, comment) %> | |
+ <%= button_to "Delete", post_comment_path(@post, comment), method: :delete %> | |
+ <%#= link_to "Delete", [:delete, comment] %> | |
+ <%#= link_to "Delete", [@post, comment], method: :delete, data: { confirm: "Are you sure?" } %> | |
+ <% end %> | |
+</div> | |
+ | |
diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb | |
index 6ac4ce2..2227350 100644 | |
--- a/app/views/posts/show.html.erb | |
+++ b/app/views/posts/show.html.erb | |
@@ -52,3 +52,9 @@ | |
<% end %> | |
<%= link_to "Back", posts_path %> | |
+<h3>Comments (<%= @post.comments.size %>)</h3> | |
+<%= render partial: "posts/comment", collection: @post.comments %> | |
+<% if user_signed_in? %> | |
+ <%= render partial: "comments/form" %> | |
+<% end %> | |
diff --git a/config/routes.rb b/config/routes.rb | |
index 9a1030b..caf3100 100644 | |
--- a/config/routes.rb | |
+++ b/config/routes.rb | |
@@ -1,7 +1,9 @@ | |
Rails.application.routes.draw do | |
devise_for :users, controllers: { registrations: "registrations" } | |
- resources :posts | |
+ resources :posts do | |
+ resources :comments | |
+ end | |
root to: "posts#index" | |
diff --git a/db/migrate/20200101000007_create_comments.rb b/db/migrate/20200101000007_create_comments.rb | |
new file mode 100644 | |
index 0000000..6395828 | |
--- /dev/null | |
+++ b/db/migrate/20200101000007_create_comments.rb | |
@@ -0,0 +1,12 @@ | |
+class CreateComments < ActiveRecord::Migration[6.0] | |
+ def change | |
+ create_table :comments do |t| | |
+ t.text :body | |
+ t.references :post, null: false, foreign_key: true | |
+ t.integer :user_id | |
+ t.string :display_name | |
+ | |
+ t.timestamps | |
+ end | |
+ end | |
+end | |
diff --git a/db/schema.rb b/db/schema.rb | |
index 913b633..050de13 100644 | |
--- a/db/schema.rb | |
+++ b/db/schema.rb | |
@@ -10,7 +10,7 @@ | |
# | |
# It's strongly recommended that you check this file into your version control system. | |
-ActiveRecord::Schema.define(version: 2020_01_01_000006) do | |
+ActiveRecord::Schema.define(version: 2020_01_01_000007) do | |
create_table "active_storage_attachments", force: :cascade do |t| | |
t.string "name", null: false | |
@@ -33,6 +33,16 @@ ActiveRecord::Schema.define(version: 2020_01_01_000006) do | |
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true | |
end | |
+ create_table "comments", force: :cascade do |t| | |
+ t.text "body" | |
+ t.integer "post_id", null: false | |
+ t.integer "user_id" | |
+ t.string "display_name" | |
+ t.datetime "created_at", precision: 6, null: false | |
+ t.datetime "updated_at", precision: 6, null: false | |
+ t.index ["post_id"], name: "index_comments_on_post_id" | |
+ end | |
+ | |
create_table "posts", force: :cascade do |t| | |
t.string "title" | |
t.string "vendor" | |
@@ -62,4 +72,5 @@ ActiveRecord::Schema.define(version: 2020_01_01_000006) do | |
end | |
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" | |
+ add_foreign_key "comments", "posts" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment