Skip to content

Commit

Permalink
Add the comment model with 1:N relationship with product
Browse files Browse the repository at this point in the history
Show the list of the comments on the product page
  • Loading branch information
vassalloandrea committed Jul 12, 2019
1 parent d7f1895 commit feac9f5
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 33 deletions.
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 :product
end
2 changes: 2 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class Product < ApplicationRecord
has_one_attached :image

has_many :comments
end
2 changes: 2 additions & 0 deletions app/views/products/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@
</p>
</div>
</div>

<%= render 'shared/comments', comments: @product.comments %>
22 changes: 22 additions & 0 deletions app/views/shared/_comments.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<h3 class="my-4">Comments</h3>

<div class="row">
<% if comments.any? %>
<% comments.each do |comment| %>
<div class="col-md-12">
<h3>
<%= comment.title %>
</h3>
<p>
<%= comment.description %>
</p>
</div>
<% end %>
<% else %>
<div class="col-md-12">
<p>
<%= t('comments.empty') %>
</p>
</div>
<% end %>
</div>
34 changes: 2 additions & 32 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
# Files in the config/locales directory are used for internationalization
# and are automatically loaded by Rails. If you want to use locales other
# than English, add the necessary files in this directory.
#
# To use the locales, use `I18n.t`:
#
# I18n.t 'hello'
#
# In views, this is aliased to just `t`:
#
# <%= t('hello') %>
#
# To use a different locale, set it with `I18n.locale`:
#
# I18n.locale = :es
#
# This would use the information in config/locales/es.yml.
#
# The following keys must be escaped otherwise they will not be retrieved by
# the default I18n backend:
#
# true, false, on, off, yes, no
#
# Instead, surround them with single quotes.
#
# en:
# 'true': 'foo'
#
# To learn more, please read the Rails Internationalization guide
# available at http://guides.rubyonrails.org/i18n.html.

en:
hello: "Hello world"
comments:
empty: There aren't comments yet.
11 changes: 11 additions & 0 deletions db/migrate/20190712114008_create_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateComments < ActiveRecord::Migration[5.2]
def change
create_table :comments do |t|
t.string :title
t.text :description
t.integer :product_id

t.timestamps
end
end
end
10 changes: 9 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: 2019_07_12_110410) do
ActiveRecord::Schema.define(version: 2019_07_12_114008) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -36,6 +36,14 @@
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end

create_table "comments", force: :cascade do |t|
t.string "title"
t.text "description"
t.integer "product_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "products", force: :cascade do |t|
t.string "name"
t.text "description"
Expand Down
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:
title: MyString
description: MyText
product_id: 1

two:
title: MyString
description: MyText
product_id: 1
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 feac9f5

Please sign in to comment.