Skip to content

Commit

Permalink
create-product
Browse files Browse the repository at this point in the history
  • Loading branch information
ShisyrMukhamed committed Jun 27, 2017
1 parent ceb8b4a commit 44fa4a3
Show file tree
Hide file tree
Showing 26 changed files with 141 additions and 16 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/products.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/products.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the products controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
23 changes: 23 additions & 0 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class ProductsController < ApplicationController
def index
@product = Product.all
end
def show
@product = Product.find(params[:id])
end
def new
@product = Product.new
end
def create
@product = Product.new(product_params)
if(@product.save)
redirect_to @product
else
render "new"
end
end
private
def product_params
params.require(:product).permit(:title, :description, :price, :sub_category_id, :image)
end
end
1 change: 1 addition & 0 deletions app/controllers/sub_categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ def index
end
def show
@sub_category = SubCategory.find(params[:id])
@products = @sub_category.products
end
def new
@sub_category = SubCategory.new
Expand Down
2 changes: 2 additions & 0 deletions app/helpers/products_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ProductsHelper
end
1 change: 0 additions & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
class Post < ApplicationRecord
belongs_to :sub_categories
end
8 changes: 8 additions & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Product < ApplicationRecord
belongs_to :sub_category
validates :title, presence: true
has_attached_file :image,
styles: {large: "300x300"},
:default_url => " /Users/Mukhamed/Downloads/instagram-master/app/assets/images/"
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
1 change: 1 addition & 0 deletions app/models/sub_category.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class SubCategory < ApplicationRecord
has_many :products
belongs_to :category
validates :title, presence: true
validates :active, presence: true
Expand Down
1 change: 0 additions & 1 deletion app/views/partials/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
<hr>
<div class="second-menu">
<%= link_to 'Меню', categories_path %> |
<%= link_to 'Создать категорию', new_category_path %> |
<%= link_to 'Регистрация', new_user_path %> |
<%= link_to 'Все статьи', posts_path %>|
<%= link_to 'Новая статья', new_post_path %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/posts/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<h1>Редактирование статьи</h1>
<h1>Редактирование продукта</h1>
<%= render 'form' %>
25 changes: 25 additions & 0 deletions app/views/products/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<h1>Creating of Product</h1>
<%= form_for @product do |pr| %>
<div class="create_products">
<%= pr.label :title %>
<%= pr.text_field :title %>
</div>
<div class="create_products">
<%= pr.label :description %>
<%= pr.text_area :description %>
</div>
<div class="create_products">
<%= pr.label :price %>
<%= pr.text_field :price %>
</div>
<div class="create_products">
<%= pr.label :sub_category_id %>
<%= pr.text_field :sub_category_id %>
</div>
<div class="create_products">
<%= pr.file_field :image %>
</div>
<div class="create_products">
<%= pr.submit "Create product" %>
</div>
<% end %>
3 changes: 3 additions & 0 deletions app/views/products/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

<h2><%= @product.title %></h2>
<%= image_tag @product.image(:large) %>
1 change: 1 addition & 0 deletions app/views/sub_categories/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

<% @sub_categories.each do |s| %>
<%= link_to s.title, s %>
<% end %>
8 changes: 8 additions & 0 deletions app/views/sub_categories/show.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
<h1><%= @sub_category.title %></h1>
<%= link_to "Создать продукт", new_product_path %>
<div class="container-products">
<% @products.each do |products|%>
<div class="col">
<%= link_to products.title, products %>
</div>
<% end %>
</div>
18 changes: 6 additions & 12 deletions app/views/users/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,32 +1,26 @@
<h1>Создать пользователь</h1>
<%= form_for @user do |f| %>
<div class="form-control">
<%= f.label :name %>
<%= f.text_field :name%>
<%= f.text_field :name, placeholder: "Name" %>
</div>
<div class="form-control">
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<%= f.text_field :last_name, placeholder: "Last_name" %>
</div>
<div class="form-control">
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.text_field :email, placeholder: "E-mail" %>
</div>
<div class="form-control">
<%= f.label :gender %>
<%= f.check_box :gender %>
</div>
<div class="form-control">
<%= f.label :age %>
<%= f.text_field :age %>
<%= f.text_field :age, placeholder: "Age" %>
</div>
<div class="form-control">
<%= f.label :phone_number %>
<%= f.text_field :phone_number %>
<%= f.text_field :phone_number, placeholder: "Phone_number" %>
</div>
<div class="form-control">
<%= f.label :password %>
<%= f.text_field :password %>
<%= f.text_field :password, placeholder: "Password" %>
</div>
<div class="form-control">
<%= f.submit "Save" %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
resources :users
resources :categories
resources :sub_categories
resources :products


end
11 changes: 11 additions & 0 deletions db/migrate/20170627043427_create_products.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateProducts < ActiveRecord::Migration[5.1]
def change
create_table :products do |t|
t.string :title
t.string :description
t.integer :price
t.integer :sub_category_id
t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20170627052108_add_images.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddImages < ActiveRecord::Migration[5.1]
def change
add_attachment :products, :image
end
end
15 changes: 14 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: 20170626170023) do
ActiveRecord::Schema.define(version: 20170627052108) do

create_table "categories", force: :cascade do |t|
t.string "title"
Expand All @@ -27,6 +27,19 @@
t.datetime "updated_at", null: false
end

create_table "products", force: :cascade do |t|
t.string "title"
t.string "description"
t.integer "price"
t.integer "sub_category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "image_file_name"
t.string "image_content_type"
t.integer "image_file_size"
t.datetime "image_updated_at"
end

create_table "sub_categories", force: :cascade do |t|
t.string "title"
t.boolean "active", default: false
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions test/controllers/products_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class ProductsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end
11 changes: 11 additions & 0 deletions test/fixtures/products.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

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
7 changes: 7 additions & 0 deletions test/models/product_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class ProductTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 44fa4a3

Please sign in to comment.