diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index e2128fd..c526a99 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -16,4 +16,8 @@ def create end render :new end + + def show + @product = Product.find_by_id params[:id] + end end diff --git a/app/models/category.rb b/app/models/category.rb new file mode 100644 index 0000000..5164e7c --- /dev/null +++ b/app/models/category.rb @@ -0,0 +1,7 @@ +class Category < ActiveRecord::Base + has_many :products + + attr_accessible :name + + validates :name, presence: true +end diff --git a/app/models/product.rb b/app/models/product.rb index 6af4143..d9acf00 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -1,6 +1,8 @@ class Product < ActiveRecord::Base before_save :set_defaults + belongs_to :category + attr_accessible :active, :description, :name, :price validates :name, presence: true diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb new file mode 100644 index 0000000..1b12552 --- /dev/null +++ b/app/views/products/show.html.erb @@ -0,0 +1,13 @@ +
+ Name: <%= @product.name %> +
++ Description: <%= @product.description %> +
++<% end %> +<%= link_to :back %> diff --git a/db/migrate/20130126203942_create_categories.rb b/db/migrate/20130126203942_create_categories.rb new file mode 100644 index 0000000..9760684 --- /dev/null +++ b/db/migrate/20130126203942_create_categories.rb @@ -0,0 +1,9 @@ +class CreateCategories < ActiveRecord::Migration + def change + create_table :categories do |t| + t.string :name + + t.timestamps + end + end +end diff --git a/db/migrate/20130126204442_add_category_id_to_product.rb b/db/migrate/20130126204442_add_category_id_to_product.rb new file mode 100644 index 0000000..0f79460 --- /dev/null +++ b/db/migrate/20130126204442_add_category_id_to_product.rb @@ -0,0 +1,5 @@ +class AddCategoryIdToProduct < ActiveRecord::Migration + def change + add_column :products, :category_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 16a87d5..3d766ee 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,13 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130126162834) do +ActiveRecord::Schema.define(:version => 20130126204442) do + + create_table "categories", :force => true do |t| + t.string "name" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end create_table "products", :force => true do |t| t.string "name" @@ -20,6 +26,7 @@ t.boolean "active" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false + t.integer "category_id" end end diff --git a/test/fixtures/categories.yml b/test/fixtures/categories.yml new file mode 100644 index 0000000..0227c60 --- /dev/null +++ b/test/fixtures/categories.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +one: + name: MyString + +two: + name: MyString diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb new file mode 100644 index 0000000..4733541 --- /dev/null +++ b/test/unit/category_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CategoryTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end