Add the migrations, add the category to the form

This commit is contained in:
Tom Naessens 2014-12-10 21:25:21 +01:00
parent 1e0f04ada2
commit e76a139205
10 changed files with 33 additions and 5 deletions

View file

@ -39,7 +39,7 @@ class ProductsController < ApplicationController
private private
def product_params def product_params
params.require(:product).permit(:name, :price, :avatar) params.require(:product).permit(:name, :price, :avatar, :category)
end end
end end

View file

@ -11,12 +11,15 @@
# avatar_content_type :string(255) # avatar_content_type :string(255)
# avatar_file_size :integer # avatar_file_size :integer
# avatar_updated_at :datetime # avatar_updated_at :datetime
# category :integer default(0)
# #
class Product < ActiveRecord::Base class Product < ActiveRecord::Base
has_many :order_products has_many :order_products
has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium
enum category: %w(food beverages other)
validates :name, presence: true validates :name, presence: true
validates :price, numericality: { only_integer: true, greater_than_or_equal_to: 0 } validates :price, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
validates_attachment :avatar, presence: true, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }, validates_attachment :avatar, presence: true, content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] },

View file

@ -16,8 +16,13 @@
# last_sign_in_at :datetime # last_sign_in_at :datetime
# current_sign_in_ip :string(255) # current_sign_in_ip :string(255)
# last_sign_in_ip :string(255) # last_sign_in_ip :string(255)
# dagschotel :reference # admin :boolean
# dagschotel_id :integer # dagschotel_id :integer
# avatar_file_name :string(255)
# avatar_content_type :string(255)
# avatar_file_size :integer
# avatar_updated_at :datetime
# orders_count :integer default(0)
# #
class User < ActiveRecord::Base class User < ActiveRecord::Base

View file

@ -8,6 +8,8 @@
<%= f.label :price %> <%= f.label :price %>
<%= f.number_field :price, value: number_with_precision(f.object.price, precision: 2), class: 'form-control', placeholder: "0.00", step: :any %> <%= f.number_field :price, value: number_with_precision(f.object.price, precision: 2), class: 'form-control', placeholder: "0.00", step: :any %>
<%= form_collection_select f, :category, Product.categories.keys, :to_s, :titlecase %>
<%= f.label :avatar %> <%= f.label :avatar %>
<%= f.file_field :avatar %> <%= f.file_field :avatar %>

View file

@ -0,0 +1,5 @@
class AddCategoryToProducts < ActiveRecord::Migration
def change
add_column :products, :category, :integer, default: 0
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20141210090544) do ActiveRecord::Schema.define(version: 20141210200008) do
create_table "order_products", force: true do |t| create_table "order_products", force: true do |t|
t.integer "order_id" t.integer "order_id"
@ -38,6 +38,7 @@ ActiveRecord::Schema.define(version: 20141210090544) do
t.string "avatar_content_type" t.string "avatar_content_type"
t.integer "avatar_file_size" t.integer "avatar_file_size"
t.datetime "avatar_updated_at" t.datetime "avatar_updated_at"
t.integer "category", default: 0
end end
create_table "users", force: true do |t| create_table "users", force: true do |t|

View file

@ -11,6 +11,7 @@
# avatar_content_type :string(255) # avatar_content_type :string(255)
# avatar_file_size :integer # avatar_file_size :integer
# avatar_updated_at :datetime # avatar_updated_at :datetime
# category :integer default(0)
# #
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

View file

@ -16,8 +16,13 @@
# last_sign_in_at :datetime # last_sign_in_at :datetime
# current_sign_in_ip :string(255) # current_sign_in_ip :string(255)
# last_sign_in_ip :string(255) # last_sign_in_ip :string(255)
# dagschotel :reference # admin :boolean
# dagschotel_id :integer # dagschotel_id :integer
# avatar_file_name :string(255)
# avatar_content_type :string(255)
# avatar_file_size :integer
# avatar_updated_at :datetime
# orders_count :integer default(0)
# #
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

View file

@ -11,6 +11,7 @@
# avatar_content_type :string(255) # avatar_content_type :string(255)
# avatar_file_size :integer # avatar_file_size :integer
# avatar_updated_at :datetime # avatar_updated_at :datetime
# category :integer default(0)
# #
require 'test_helper' require 'test_helper'

View file

@ -16,8 +16,13 @@
# last_sign_in_at :datetime # last_sign_in_at :datetime
# current_sign_in_ip :string(255) # current_sign_in_ip :string(255)
# last_sign_in_ip :string(255) # last_sign_in_ip :string(255)
# dagschotel :reference # admin :boolean
# dagschotel_id :integer # dagschotel_id :integer
# avatar_file_name :string(255)
# avatar_content_type :string(255)
# avatar_file_size :integer
# avatar_updated_at :datetime
# orders_count :integer default(0)
# #
require 'test_helper' require 'test_helper'