Skip to content

Commit

Permalink
Commit 2. -Finished I2 of tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
BorisJov committed Aug 6, 2017
1 parent 61141c7 commit 8fa572e
Show file tree
Hide file tree
Showing 17 changed files with 245 additions and 51 deletions.
189 changes: 140 additions & 49 deletions .idea/workspace.xml

Large diffs are not rendered by default.

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/
4 changes: 4 additions & 0 deletions app/controllers/articles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def index

def show
@article = Article.find(params[:id])

@comment = Comment.new
@comment.article_id = @article.id

end

def new
Expand Down
15 changes: 15 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -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
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
1 change: 1 addition & 0 deletions app/models/article.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Article < ApplicationRecord
has_many :comments
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 < ApplicationRecord
belongs_to :article
end
5 changes: 5 additions & 0 deletions app/views/articles/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
<h5>Comment by <%= comment.author_name %></h5>
<p class="comment"><%= comment.body %></p>
<p>Posted <%= distance_of_time_in_words(comment.created_at, Time.now) %> ago</p><br/>
</div>
6 changes: 6 additions & 0 deletions app/views/articles/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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?'} %>

<h3>Comments (<%= @article.comments.size %>)</h3>
<%= render partial: 'articles/comment', collection: @article.comments %>

<%= render partial: 'comments/form' %>

<%= link_to '<< Back to Articles List', articles_path %>
14 changes: 14 additions & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h3>Post a comment:</h3>
<%= form_for [ @article, @comment ] do |f| %>
<p>
<%= f.label :author_name, "Your name" %><br/>
<%= f.text_field :author_name %>
</p>
<p>
<%= f.label :body, "Your text here:" %><br/>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit 'Submit' %>
</p>
<% end %>
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions db/migrate/20170806133537_create_comments.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
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 < ActionDispatch::IntegrationTest
# 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:
author_name: MyString
body: MyText
article: one

two:
author_name: MyString
body: MyText
article: two
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 8fa572e

Please sign in to comment.