Skip to content

Commit

Permalink
Add commentsgit add .
Browse files Browse the repository at this point in the history
  • Loading branch information
muditabaid committed Nov 18, 2017
1 parent 0840c64 commit 0c9ba97
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 2 deletions.
Empty file added _formhtml.erb
Empty file.
3 changes: 3 additions & 0 deletions app/assets/javascripts/comments.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/comments.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Comments controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
16 changes: 16 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CommentsController < ApplicationController

def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment].permit(:name, :body))

redirect_to post_path(@post)
end
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy

redirect_to post_path(@post)
end
end
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
3 changes: 3 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Comment < ActiveRecord::Base
belongs_to :post
end
1 change: 1 addition & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Post < ActiveRecord::Base
has_many :comments, dependent: :destroy
validates :title, presence: true, length: { minimum: 5 }
validates :body, presence: true
end
13 changes: 13 additions & 0 deletions app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="comment clearfix">
<div class="comment_content">
<p class="comment_name"><strong><%= comment.name %></strong></p>
<p class="comment_body"><%= comment.body %></p>
<p class="comment_time"><%= time_ago_in_words(comment.created_at) %> Ago</p>
</div>

<p><%= link_to 'Delete', [comment.post, comment],
method: :delete,
class: "button",
data: { confirm: 'Are you sure?' } %></p>

</div>
16 changes: 16 additions & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<%= form_for([@post, @post.comments.build]) do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>

</p>
<p>
<%= f.label :body%><br>
<%= f.text_area :body %>

</p>
<br>
<p>
<%= f.submit%>
</p>
<% end %>
7 changes: 7 additions & 0 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@
<p class="body">
<%= @post.body %>
</p>
<div id="comments">
<h2><%= @post.comments.count %> Comments </h2>
<%= render @post.comments%>

<h3>Add a comment:</h3>
<%= render "comments/form"%>
</div>
</div>
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Rails.application.routes.draw do
#get 'welcome/index'
resources :posts
resources :posts do
resources :comments
end

root "posts#index"

# The priority is based upon order of creation: first created -> highest priority.
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20171118174853_create_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :name
t.text :body
t.references :post, index: true, foreign_key: true

t.timestamps null: false
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20171118102948) do
ActiveRecord::Schema.define(version: 20171118174853) do

create_table "comments", force: :cascade do |t|
t.string "name"
t.text "body"
t.integer "post_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "comments", ["post_id"], name: "index_comments_on_post_id"

create_table "posts", force: :cascade do |t|
t.string "title"
Expand Down
7 changes: 7 additions & 0 deletions test/controllers/comments_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class CommentsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end
11 changes: 11 additions & 0 deletions test/fixtures/comments.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
name: MyString
body: MyText
post_id:

two:
name: MyString
body: MyText
post_id:
7 changes: 7 additions & 0 deletions test/models/comment_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class CommentTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 0c9ba97

Please sign in to comment.