Skip to content

Instantly share code, notes, and snippets.

@jtperreault
Created November 7, 2016 15:50
Show Gist options
  • Save jtperreault/4d7ae00d7c48128ab49298e76fc69bf9 to your computer and use it in GitHub Desktop.
Save jtperreault/4d7ae00d7c48128ab49298e76fc69bf9 to your computer and use it in GitHub Desktop.
<div class="row">
<% @categories.each do |category|%>
<h2><%=category.name%></h2>
<%= image_tag category.image.thumb.url%>
<ul>
<%= category.products.published.each do |product| %>
<li><%=product.name%></li>
<li><%=product.price%></li>
<li><%=product.description%></li>
<% end %>
</br>
</ul>
<% end %>
</div>
class Product < ApplicationRecord
scope :published, -> {
where(:is_published => true)
}
belongs_to :category
validates_presence_of :name, :price
mount_uploader :image, ImageUploader
validates_processing_of :image
validate :image_size_validation
private
def image_size_validation
errors[:image] << "Η εικόνα πρέπει να είναι μικρότερη απο 3MB." if image.size > 3.megabytes
end
end
class ProductsController < ApplicationController
def index
@categories = Category.published.order("position")
@products = Product.where(is_published: true).order("position")
end
def show
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment