I want to be able to show 3 random product images below a product image, so far I've inserted this code below the product image as seen in the views/products/show.html.erb
views/products/show.html.erb
<div class="col-xs-12 col-sm-6 center-block" >
<%= image_tag @product.image.url(:medium), class: "img-responsive" %>
#This is the Product image#
##EDITED ##
#Below is the codesnippet for three products to appear##
<% @products.each do |product| %>
<div class="col-sm-2 center-block " >
<%= link_to product_path (product) do %>
<%= image_tag product.image.url(:thumb), class: "img-responsive" %>
<% end %>
<div class="product_description">
<h5><%= link_to product.title, product %></h5>
</div>
</div>
<% end %>
And in my products_controller.rb
I've the code, I believe the problem is to be found in the @products
, but I'm not sure what it is. Please can someone advice me.
products_controller.rb
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def show
@meta_title = "Concept Store #{@product.title}"
@meta_description = @product.description
@products = Product.all ###EDITED###
end
private
# Use callbacks to share common setup or constraints between actions.
def set_product
@product = Product.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def product_params
params.require(:product).permit(:title, :description, :price_usd, :price_isl, :image, :category_id, :stock_quantity, :label_id, :query, :slug)
end
end
My models Products
and Categories
have relations
product.rb
class Product < ActiveRecord::Base
belongs_to :category
belongs_to :label
has_many :product_items, :dependent => :destroy
extend FriendlyId
friendly_id :title, use: [:slugged, :finders]
validates :title, :description, presence: true
validates :price_usd, :price_isl, numericality: {greater_than_or_equal_to: 0.01}
validates :title, uniqueness: true
has_attached_file :image, styles: { medium: "500x500#", thumb: "100x100#" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end
category.rb
class Category < ActiveRecord::Base
has_many :products, :dependent => :nullify
extend FriendlyId
friendly_id :name, use: [:slugged, :finders]
end