Add paperclip to gems and avatar to products
This commit is contained in:
parent
182932abf5
commit
552f811fea
15 changed files with 74 additions and 29 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -14,3 +14,6 @@
|
|||
# Ignore all logfiles and tempfiles.
|
||||
/log/*.log
|
||||
/tmp
|
||||
|
||||
# Avatars of producst
|
||||
public/system/products
|
||||
|
|
3
Gemfile
3
Gemfile
|
@ -62,3 +62,6 @@ gem 'bcrypt', '3.1.7'
|
|||
#paginate stuff
|
||||
gem 'will_paginate', '3.0.7'
|
||||
gem 'bootstrap-will_paginate', '0.0.10'
|
||||
|
||||
# paperclip for easy file attachment
|
||||
gem 'paperclip'
|
||||
|
|
10
Gemfile.lock
10
Gemfile.lock
|
@ -54,6 +54,10 @@ GEM
|
|||
capistrano-rvm (0.1.2)
|
||||
capistrano (~> 3.0)
|
||||
sshkit (~> 1.2)
|
||||
climate_control (0.0.3)
|
||||
activesupport (>= 3.0)
|
||||
cocaine (0.5.4)
|
||||
climate_control (>= 0.0.3, < 1.0)
|
||||
coffee-rails (4.0.1)
|
||||
coffee-script (>= 2.2.0)
|
||||
railties (>= 4.0.0, < 5.0)
|
||||
|
@ -84,6 +88,11 @@ GEM
|
|||
net-scp (1.2.1)
|
||||
net-ssh (>= 2.6.5)
|
||||
net-ssh (2.9.1)
|
||||
paperclip (4.2.0)
|
||||
activemodel (>= 3.0.0)
|
||||
activesupport (>= 3.0.0)
|
||||
cocaine (~> 0.5.3)
|
||||
mime-types
|
||||
rack (1.5.2)
|
||||
rack-test (0.6.2)
|
||||
rack (>= 1.0)
|
||||
|
@ -158,6 +167,7 @@ DEPENDENCIES
|
|||
jbuilder (~> 2.0)
|
||||
jquery-rails
|
||||
mysql2
|
||||
paperclip
|
||||
rails (= 4.1.7)
|
||||
sass-rails (~> 4.0.3)
|
||||
sdoc (~> 0.4.0)
|
||||
|
|
|
@ -8,7 +8,7 @@ class ProductsController < ApplicationController
|
|||
if @product.save
|
||||
redirect_to @product
|
||||
else
|
||||
render "new"
|
||||
render 'new'
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -42,7 +42,7 @@ class ProductsController < ApplicationController
|
|||
|
||||
def product_params
|
||||
params.require(:product).permit(:name, :purchase_price, :sale_price,
|
||||
:img_path)
|
||||
:avatar)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -2,19 +2,24 @@
|
|||
#
|
||||
# Table name: products
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# name :string(255)
|
||||
# purchase_price :integer
|
||||
# sale_price :integer
|
||||
# img_path :string(255)
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# id :integer not null, primary key
|
||||
# name :string(255)
|
||||
# purchase_price :integer
|
||||
# sale_price :integer
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# avatar_file_name :string(255)
|
||||
# avatar_content_type :string(255)
|
||||
# avatar_file_size :integer
|
||||
# avatar_updated_at :datetime
|
||||
#
|
||||
|
||||
class Product < ActiveRecord::Base
|
||||
has_one :order_product
|
||||
has_attached_file :avatar, styles: { medium: "100x100>" }, default_style: :medium
|
||||
|
||||
validates :name, presence: true
|
||||
validates :purchase_price, numericality: { only_integer: true, greater_than_or_equal_to: 0 }
|
||||
validates :sale_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"] }
|
||||
end
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
</button>
|
||||
</span>
|
||||
<%= product_field.text_field :count, class: 'form-control', value: 0 %>
|
||||
<%= image_tag op.product.avatar %>
|
||||
<span class="input-group-btn">
|
||||
<button class="btn btn-default btn-inc" type="button">
|
||||
<span class="glyphicon glyphicon-plus"></span>
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
<%= form_for @product do |f| %>
|
||||
<%= form_for @product, html: { multipart: true } do |f| %>
|
||||
<%= render 'application/errors', model: @product %>
|
||||
|
||||
<%= f.label :name %>
|
||||
<%= f.text_field :name %>
|
||||
|
||||
|
@ -8,8 +10,8 @@
|
|||
<%= f.label :sale_price %>
|
||||
<%= f.number_field :sale_price %>
|
||||
|
||||
<%= f.label :img_path %>
|
||||
<%= f.text_field :img_path %>
|
||||
<%= f.label :avatar %>
|
||||
<%= f.file_field :avatar %>
|
||||
|
||||
<%= f.submit "Create product", class: "btn btn-primary" %>
|
||||
<% end %>
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
<p>Name: <%= @product.name %></p>
|
||||
<p>Purchase price: <%= @product.purchase_price %> </p>
|
||||
<p>Sale price: <%= @product.sale_price %> </p>
|
||||
<p>Img path: <%= @product.img_path %></p>
|
||||
<p><%= image_tag @product.avatar %></p>
|
||||
<%= link_to "edit", edit_product_path(@product) %>
|
||||
|
||||
</aside>
|
||||
|
|
|
@ -34,4 +34,6 @@ Rails.application.configure do
|
|||
|
||||
# Raises error for missing translations
|
||||
# config.action_view.raise_on_missing_translations = true
|
||||
|
||||
Paperclip.options[:command_path] = "/usr/local/bin/"
|
||||
end
|
||||
|
|
|
@ -75,4 +75,6 @@ Rails.application.configure do
|
|||
|
||||
# Do not dump schema after migrations.
|
||||
config.active_record.dump_schema_after_migration = false
|
||||
|
||||
Paperclip.options[:command_path] = "/usr/local/bin/"
|
||||
end
|
||||
|
|
|
@ -36,4 +36,6 @@ Rails.application.configure do
|
|||
|
||||
# Raises error for missing translations
|
||||
# config.action_view.raise_on_missing_translations = true
|
||||
|
||||
Paperclip.options[:command_path] = "/usr/local/bin/"
|
||||
end
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
class ReplaceProductImageByPaperclipAttachment < ActiveRecord::Migration
|
||||
def change
|
||||
remove_column :products, :img_path, :string
|
||||
add_attachment :products, :avatar
|
||||
end
|
||||
end
|
|
@ -11,7 +11,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20141128123152) do
|
||||
ActiveRecord::Schema.define(version: 20141204191328) do
|
||||
|
||||
create_table "order_products", force: true do |t|
|
||||
t.integer "order_id"
|
||||
|
@ -33,9 +33,12 @@ ActiveRecord::Schema.define(version: 20141128123152) do
|
|||
t.string "name"
|
||||
t.integer "purchase_price"
|
||||
t.integer "sale_price"
|
||||
t.string "img_path"
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.string "avatar_file_name"
|
||||
t.string "avatar_content_type"
|
||||
t.integer "avatar_file_size"
|
||||
t.datetime "avatar_updated_at"
|
||||
end
|
||||
|
||||
create_table "users", force: true do |t|
|
||||
|
|
17
test/fixtures/products.yml
vendored
17
test/fixtures/products.yml
vendored
|
@ -2,13 +2,16 @@
|
|||
#
|
||||
# Table name: products
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# name :string(255)
|
||||
# purchase_price :integer
|
||||
# sale_price :integer
|
||||
# img_path :string(255)
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# id :integer not null, primary key
|
||||
# name :string(255)
|
||||
# purchase_price :integer
|
||||
# sale_price :integer
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# avatar_file_name :string(255)
|
||||
# avatar_content_type :string(255)
|
||||
# avatar_file_size :integer
|
||||
# avatar_updated_at :datetime
|
||||
#
|
||||
|
||||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
|
|
@ -2,13 +2,16 @@
|
|||
#
|
||||
# Table name: products
|
||||
#
|
||||
# id :integer not null, primary key
|
||||
# name :string(255)
|
||||
# purchase_price :integer
|
||||
# sale_price :integer
|
||||
# img_path :string(255)
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# id :integer not null, primary key
|
||||
# name :string(255)
|
||||
# purchase_price :integer
|
||||
# sale_price :integer
|
||||
# created_at :datetime
|
||||
# updated_at :datetime
|
||||
# avatar_file_name :string(255)
|
||||
# avatar_content_type :string(255)
|
||||
# avatar_file_size :integer
|
||||
# avatar_updated_at :datetime
|
||||
#
|
||||
|
||||
require 'test_helper'
|
||||
|
|
Loading…
Reference in a new issue