고객이 제품을 선택하고 products/show.html.erb
에서 해당 제품을 아래 div에서 볼 수 있어야합니다.ruby on rails app에 무작위 제품 표시
category.rb 및 product.rb는 관련이 있습니다
cateory.rb has_many :products, :dependent => :nullify
product.rb belongs_to :category belongs_to :label
그래서 필자는 그렇게 할 수있는 가장 좋은 방법을 생각하면 선택한 제품으로 같은 범주에서 임의로 제품을 표시하는 것입니다 belongs_to
? 순간
이 내가 선택한 제품과 동일한 카테고리의 제품 만 잡아 그것을 필요로하는 products_controller.rb
@products_rand = Product.offset(offset).limit(6)
의 def show
라인이 무엇인지?
products_controller.rb
class ProductsController < ApplicationController
before_action :set_product, only: [:show, :edit, :update, :destroy]
def show
offset = rand(100)
@meta_title = "Mypage #{@product.title}"
@meta_description = @product.description
@products_rand = Product.offset(offset).limit(6)
end
private
def set_product
@product = Product.find(params[:id])
end
def product_params
params.require(:product).permit(:title, :description, :image, :category_id, :stock_quantity, :label_id, :query, :slug)
end
end
그리고 임의의 제품이 여기에
views/products/show.html.erb
<div class="row product-teaser">
<h4 class="text-center teaser-text"> Products from same category </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 %>
<%= image_tag product.image.url, :size => "100%x100%", class: "img-responsive center-block" %>
<% end %>
<h5 class="text-center"><%= link_to product.title, product, class: "text-center" %></h5>
</div>
<% end %>
</div>
이 표시됩니다 약 앞의 질문에 대한 링크입니다이 Not able to display random images below product image in Ruby on rails app?
감사의 포스트 그레스 버전 작동합니다. 선택한 제품을 무작위 제품에서 제외시킬 수 있습니까? – Slowboy
'where.not. (id : @ product.id)'를 사용할 수 있습니다. – Shannon
'where.not.' 대신'where.not.'을 사용하지 않았습니까? – Slowboy