0

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 Categorieshave 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
Slowboy
  • 581
  • 1
  • 7
  • 25
  • I don't think `each_slice` does what you think. [Check out the docs](https://ruby-doc.org/core-2.2.0/Enumerable.html#method-i-each_slice). – coreyward May 11 '17 at 16:15

1 Answers1

1

Let's break this down:

@products = Category.joins(:products).where(:products => {:id => @product.image})
  • @products okay, presumably a collection of Product instances
  • Category.joins(:products) uhhh, this returns instances of Category
  • where(products: { id: @product.image }) wait, filter by products with an ID matching @product.image, which is ostensibly a string url or file path

All together: @products = an array of categories with associated products where the numeric ID matches a particular string on a particular product.

My recommendation for you would be to review the Rails Guides with particular attention to the ActiveRecord Basics.

coreyward
  • 77,547
  • 20
  • 137
  • 166
  • thanks, I was complicating things to much. I ended with just doing `@products = Product.all` still I have to figure out how to make only 3 image appear randomly – Slowboy May 11 '17 at 16:36
  • 1
    http://stackoverflow.com/questions/2752231/random-record-in-activerecord – coreyward May 11 '17 at 16:38