Skip to content

Commit aea9608

Browse files
committed
Added some validations to the Product model
1 parent 6b8a3fb commit aea9608

File tree

8 files changed

+24
-29
lines changed

8 files changed

+24
-29
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ end
3434

3535
gem 'foundation-rails'
3636
gem "paperclip", "~> 4.1"
37+
gem 'pry'
38+
gem 'pry-nav'
3739

3840
# Use ActiveModel has_secure_password
3941
# gem 'bcrypt-ruby', '~> 3.1.2'

Gemfile.lock

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ GEM
3131
activesupport (>= 3.0)
3232
cocaine (0.5.4)
3333
climate_control (>= 0.0.3, < 1.0)
34+
coderay (1.1.0)
3435
coffee-rails (4.0.1)
3536
coffee-script (>= 2.2.0)
3637
railties (>= 4.0.0, < 5.0)
@@ -55,6 +56,7 @@ GEM
5556
mail (2.5.4)
5657
mime-types (~> 1.16)
5758
treetop (~> 1.4.8)
59+
method_source (0.8.2)
5860
mime-types (1.25.1)
5961
minitest (4.7.5)
6062
multi_json (1.9.2)
@@ -64,6 +66,12 @@ GEM
6466
cocaine (~> 0.5.3)
6567
mime-types
6668
polyglot (0.3.4)
69+
pry (0.9.12.4)
70+
coderay (~> 1.0)
71+
method_source (~> 0.8)
72+
slop (~> 3.4)
73+
pry-nav (0.2.3)
74+
pry (~> 0.9.10)
6775
rack (1.5.2)
6876
rack-test (0.6.2)
6977
rack (>= 1.0)
@@ -92,6 +100,7 @@ GEM
92100
sdoc (0.4.0)
93101
json (~> 1.8)
94102
rdoc (~> 4.0, < 5.0)
103+
slop (3.4.7)
95104
sprockets (2.11.0)
96105
hike (~> 1.2)
97106
multi_json (~> 1.0)
@@ -124,6 +133,8 @@ DEPENDENCIES
124133
jbuilder (~> 1.2)
125134
jquery-rails
126135
paperclip (~> 4.1)
136+
pry
137+
pry-nav
127138
rails (= 4.0.2)
128139
sass-rails (~> 4.0.0)
129140
sdoc

app/controllers/products_controller.rb

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
require 'pry'
2+
require 'pry-nav'
3+
14
class ProductsController < ApplicationController
25
before_action :set_product, only: [:show, :edit, :update, :destroy]
36

@@ -17,10 +20,6 @@ def new
1720
@product = Product.new
1821
end
1922

20-
# GET /products/1/edit
21-
def edit
22-
end
23-
2423
# POST /products
2524
# POST /products.json
2625
def create
@@ -37,20 +36,6 @@ def create
3736
end
3837
end
3938

40-
# PATCH/PUT /products/1
41-
# PATCH/PUT /products/1.json
42-
def update
43-
respond_to do |format|
44-
if @product.update(product_params)
45-
format.html { redirect_to @product, notice: 'Product was successfully updated.' }
46-
format.json { head :no_content }
47-
else
48-
format.html { render action: 'edit' }
49-
format.json { render json: @product.errors, status: :unprocessable_entity }
50-
end
51-
end
52-
end
53-
5439
# DELETE /products/1
5540
# DELETE /products/1.json
5641
def destroy

app/models/product.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
class Product < ActiveRecord::Base
22

3-
has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "150x150>" }, :default_url => "/images/:style/missing.png"
3+
has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "150x150>" }
44
validates_attachment_content_type :photo, :content_type => /\Aimage\/.*\Z/
55

6+
validates :name, presence: true
7+
validates :description, presence: true
8+
validates :photo, presence: true
9+
610
end

app/views/products/_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</div>
2626
<div class="field">
2727
<%= f.label :photo %>
28-
<%= f.file_field :photo %>
28+
<%= f.file_field :photo , :required => 'required' %>
2929
</div>
3030
<div class="actions">
3131
<%= f.submit :class => 'button' %>

app/views/products/edit.html.erb

Lines changed: 0 additions & 6 deletions
This file was deleted.

app/views/products/new.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
<%= render 'form' %>
44

5-
<%= link_to 'Back', products_path %>
5+
<%= link_to 'Home', products_path %>

app/views/products/show.html.erb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,5 @@
1717
<%= @product.description %>
1818
</p>
1919

20-
<%= link_to 'Edit', edit_product_path(@product) %> |
2120
<%= link_to 'Delete', @product, method: :delete %> |
22-
<%= link_to 'Back', products_path %>
21+
<%= link_to 'Home', products_path %>

0 commit comments

Comments
 (0)