【发布时间】:2017-09-29 14:12:33
【问题描述】:
我正在使用此代码显示所选产品所在类别中相似产品的随机图像。
<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>
问题是loop 中的产品都显示了一个选定产品的图像,但产品的名称和链接是正确的。
这张图片显示了我的意思,客户选择了一款名为 ATLAS 的产品,并且类似产品(来自同一 category 的产品)与 Atlas 图片一起显示。
这里是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
【问题讨论】:
标签: ruby-on-rails ruby loops random