Pt-E-3
Menachem says:
I didn’t find any pages to update. But I did remove the views/line_items directory, as it allows the user to interact with the line_items model directly
fkiss says:
I set the image_url of products to be unique:
validates :image_url, uniqueness: true
and wrote tests to verify that behaviour (similar to unique title tests)
...in depot/test/unit/product_test.rb
def new_product_with_url(image_url)
Product.new(
title: "My Book Title",
description: "yyy",
price: 1,
image_url: image_url
)
end
test "product is not valid without a unique image_url" do
product = new_product_with_url(products(:ruby).image_url)
assert !product.save
assert_equal "has already been taken", product.errors[:image_url].join('; ')
end
GreenDavidDude says:
I made the following changes to depot/app/models/product.rb:
validates :image_url, :uniqueness => true
validates :price, :numericality => {:less_than_or_equal_to => 1000}
CarlosR says:
In addition to GreenDavidDude’s changes, I added exception handlers to the LineItemsController and ProductsController
...in depot/app/controllers/line_items_controller.rb
def show
@line_item = LineItem.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @line_item }
end
rescue ActiveRecord::RecordNotFound
logger.error "Attempt to access invalid line_item #{ params[ :id ]}"
redirect_to store_url, :notice => 'Invalid line item'
end
...in depot/app/controllers/products_controller.rb
def show
@product = Product.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @product }
end
rescue ActiveRecord::RecordNotFound
logger.error "Attempt to access product #{ params[ :id ]}"
redirect_to products_url, :notice => 'Invalid product'
end

