vendredi 29 septembre 2017

Random Images in Ruby on rails 4 App

I am using this code to display random images for similar products in the same category the selected product is in.

 <div class="row container product-teaser">
   <h4 class="text-center teaser-text"> similar products to <%= @product.title %> : </h4>

     <% @products_rand.each do |product| %>
       <div class="col-sm-2 col-xs-3 center-block product-thumbs-product-view" >
          <%= link_to product_path (product) do %>
             <% if product.images.first %>
          <%= image_tag @product.images.first.image.url(:medium), :size => "100%x100%", class: "img-responsive center-block" %>
          <% end %>        
      <% end %>
      <h5 class="text-center"><%= link_to product.title, product, class: "text-center" %></h5>
  </div>
    <% end %>
 </div>

The problem is that the products in this loop are all displaying the image for the one selected product but the names and the link for the products are correct.

THis picture shows what I mean, the Customer have selected a product called ATLAS and similar products ( products from the same category) are displaying with the Atlas picture.

enter image description here

here is the product_controller.rb

 class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]


  def show

  offset = rand(100)
  @meta_title = "Concept Store #{@product.title}"
  @meta_description = @product.description
  @products_rand = Product.where(category_id: @product.category_id).order("RANDOM()").limit(6)
 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, :image, :category_id, :stock_quantity, :label_id, :query, :slug, images_attributes: [:image , :id , :_destroy])
end


end




Aucun commentaire:

Enregistrer un commentaire