Commit 1df0b000 by vulehuan

add new product featured

parent 61d967f4
...@@ -21,13 +21,25 @@ class ProductsController < ApplicationController ...@@ -21,13 +21,25 @@ class ProductsController < ApplicationController
@product = Product.new @product = Product.new
end end
def create
@product = Product.new(product_params)
user = current_user
@product.user_id = user.id
if @product.save
flash[:success] = "Add new product successfully!"
redirect_to @product
else
render 'new'
end
end
def user_items def user_items
end end
private private
def product_params def product_params
params.require(:product).permit(:name, :description, :headline, :availability, :code, :condition, :image_small, :image_medium, :price_currency, :price) params.require(:product).permit(:name, :description, :headline, :availability, :code, :condition, :image_small, :image_medium, :price_currency, :price, :product_category_id)
end end
# Before filters # Before filters
......
class Product < ActiveRecord::Base class Product < ActiveRecord::Base
validates :name, :availability, :code, :condition, :image_medium, :price, :price_currency, presence: true
belongs_to :product_category
# Returns recommended products (order by rate, and available) # Returns recommended products (order by rate, and available)
def self.get_recommended_products(options = { limit: 8 }) def self.get_recommended_products(options = { limit: 8 })
limit = options[:limit] limit = options[:limit]
......
class ProductCategory < ActiveRecord::Base class ProductCategory < ActiveRecord::Base
has_many :product
Please register or sign in to reply
end end
\ No newline at end of file
<%= render 'shared/error_messages', entity: @product %> <%= render 'shared/error_messages', entity: @product %>
<%= f.label :product_category_id %>
<%= collection_select(:product, :product_category_id, ProductCategory.all, :id, :name, {}, class: 'form-control') %>
<%= f.label :name %> <%= f.label :name %>
<%= f.text_field :name, class: "form-control" %> <%= f.text_field :name, class: "form-control" %>
......
...@@ -15,6 +15,7 @@ class CreateProducts < ActiveRecord::Migration ...@@ -15,6 +15,7 @@ class CreateProducts < ActiveRecord::Migration
t.decimal :price t.decimal :price
t.integer :product_category_id t.integer :product_category_id
t.integer :user_id t.integer :user_id
t.boolean :status
t.timestamps t.timestamps
end end
......
...@@ -48,6 +48,7 @@ ActiveRecord::Schema.define(version: 20131024162951) do ...@@ -48,6 +48,7 @@ ActiveRecord::Schema.define(version: 20131024162951) do
t.decimal "price", precision: 10, scale: 0 t.decimal "price", precision: 10, scale: 0
t.integer "product_category_id" t.integer "product_category_id"
t.integer "user_id" t.integer "user_id"
t.boolean "status"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
end end
......
...@@ -92,7 +92,8 @@ namespace :db do ...@@ -92,7 +92,8 @@ namespace :db do
review_count: review_count, review_count: review_count,
price_currency: price_currency, price_currency: price_currency,
price: price, price: price,
product_category_id: category_id product_category_id: category_id,
status: 1
) )
end end
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment