-
-
+
+
diff --git a/app/assets/javascripts/comments.coffee b/app/assets/javascripts/comments.coffee
new file mode 100644
index 0000000..24f83d1
--- /dev/null
+++ b/app/assets/javascripts/comments.coffee
@@ -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/
diff --git a/app/assets/stylesheets/comments.scss b/app/assets/stylesheets/comments.scss
new file mode 100644
index 0000000..3722c12
--- /dev/null
+++ b/app/assets/stylesheets/comments.scss
@@ -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/
diff --git a/app/controllers/articles_controller.rb b/app/controllers/articles_controller.rb
index 65e7328..8a6f00e 100644
--- a/app/controllers/articles_controller.rb
+++ b/app/controllers/articles_controller.rb
@@ -7,6 +7,10 @@ def index
def show
@article = Article.find(params[:id])
+
+ @comment = Comment.new
+ @comment.article_id = @article.id
+
end
def new
diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb
new file mode 100644
index 0000000..80cfe91
--- /dev/null
+++ b/app/controllers/comments_controller.rb
@@ -0,0 +1,15 @@
+class CommentsController < ApplicationController
+ def create
+ @comment = Comment.new(comment_params)
+ @comment.article_id = params[:article_id]
+
+ @comment.save
+
+ redirect_to article_path(@comment.article)
+ end
+
+ def comment_params
+ params.require(:comment).permit(:author_name, :body)
+ end
+
+end
diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb
new file mode 100644
index 0000000..0ec9ca5
--- /dev/null
+++ b/app/helpers/comments_helper.rb
@@ -0,0 +1,2 @@
+module CommentsHelper
+end
diff --git a/app/models/article.rb b/app/models/article.rb
index b7a72b5..900c54e 100644
--- a/app/models/article.rb
+++ b/app/models/article.rb
@@ -1,2 +1,3 @@
class Article < ApplicationRecord
+ has_many :comments
end
diff --git a/app/models/comment.rb b/app/models/comment.rb
new file mode 100644
index 0000000..889a476
--- /dev/null
+++ b/app/models/comment.rb
@@ -0,0 +1,3 @@
+class Comment < ApplicationRecord
+ belongs_to :article
+end
diff --git a/app/views/articles/_comment.html.erb b/app/views/articles/_comment.html.erb
new file mode 100644
index 0000000..7e9eb4f
--- /dev/null
+++ b/app/views/articles/_comment.html.erb
@@ -0,0 +1,5 @@
+
+
Comment by <%= comment.author_name %>
+
+
Posted <%= distance_of_time_in_words(comment.created_at, Time.now) %> ago
+
\ No newline at end of file
diff --git a/app/views/articles/show.html.erb b/app/views/articles/show.html.erb
index bb44324..c8d6a96 100644
--- a/app/views/articles/show.html.erb
+++ b/app/views/articles/show.html.erb
@@ -3,4 +3,10 @@
<%= link_to 'Edit Article', edit_article_path(@article) %>
<%= link_to 'Delete Article', article_path(@article), method: :delete, data: {confirm: 'Are you sure it is wise to delete the article?'} %>
+
+Comments (<%= @article.comments.size %>)
+<%= render partial: 'articles/comment', collection: @article.comments %>
+
+<%= render partial: 'comments/form' %>
+
<%= link_to '<< Back to Articles List', articles_path %>
diff --git a/app/views/comments/_form.html.erb b/app/views/comments/_form.html.erb
new file mode 100644
index 0000000..d0f9a5b
--- /dev/null
+++ b/app/views/comments/_form.html.erb
@@ -0,0 +1,14 @@
+Post a comment:
+<%= form_for [ @article, @comment ] do |f| %>
+
+ <%= f.label :author_name, "Your name" %>
+ <%= f.text_field :author_name %>
+
+
+ <%= f.label :body, "Your text here:" %>
+ <%= f.text_area :body %>
+
+
+ <%= f.submit 'Submit' %>
+
+<% end %>
\ No newline at end of file
diff --git a/config/routes.rb b/config/routes.rb
index d0a3960..66009e5 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,5 +1,7 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
root to: 'articles#index'
- resources :articles
+ resources :articles do
+ resources :comments
+ end
end
diff --git a/db/migrate/20170806133537_create_comments.rb b/db/migrate/20170806133537_create_comments.rb
new file mode 100644
index 0000000..774d9ea
--- /dev/null
+++ b/db/migrate/20170806133537_create_comments.rb
@@ -0,0 +1,11 @@
+class CreateComments < ActiveRecord::Migration[5.1]
+ def change
+ create_table :comments do |t|
+ t.string :author_name
+ t.text :body
+ t.references :article, foreign_key: true
+
+ t.timestamps
+ end
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 2546d58..28acd75 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: 20170802132923) do
+ActiveRecord::Schema.define(version: 20170806133537) do
create_table "articles", force: :cascade do |t|
t.string "title"
@@ -19,4 +19,13 @@
t.datetime "updated_at", null: false
end
+ create_table "comments", force: :cascade do |t|
+ t.string "author_name"
+ t.text "body"
+ t.integer "article_id"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["article_id"], name: "index_comments_on_article_id"
+ end
+
end
diff --git a/test/controllers/comments_controller_test.rb b/test/controllers/comments_controller_test.rb
new file mode 100644
index 0000000..a812dda
--- /dev/null
+++ b/test/controllers/comments_controller_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class CommentsControllerTest < ActionDispatch::IntegrationTest
+ # test "the truth" do
+ # assert true
+ # end
+end
diff --git a/test/fixtures/comments.yml b/test/fixtures/comments.yml
new file mode 100644
index 0000000..50bcc5b
--- /dev/null
+++ b/test/fixtures/comments.yml
@@ -0,0 +1,11 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ author_name: MyString
+ body: MyText
+ article: one
+
+two:
+ author_name: MyString
+ body: MyText
+ article: two
diff --git a/test/models/comment_test.rb b/test/models/comment_test.rb
new file mode 100644
index 0000000..b6d6131
--- /dev/null
+++ b/test/models/comment_test.rb
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class CommentTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end