无法在Ruby on Rails应用程序中显示产品图像下方的随机图像? [英] Not able to display random images below product image in Ruby on rails app?

查看:128
本文介绍了无法在Ruby on Rails应用程序中显示产品图像下方的随机图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在产品图片下方显示3个随机产品图片,到目前为止,我已经在产品图片的下方插入了该代码,如 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%>
#这是产品图片#
## EDITED ##
#Below是三款产品的代码片段## b $ b<%@ products.each do | product | %GT;
< 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%>

在我的 products_controller.rb 我相信问题是在 @products 中找到的,但我不确定它是什么。

products_controller.rb

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

$ b def show
@meta_title =Concept Store#{ @ product.title}
@meta_description = @ product.description
@products = Product.all ### EDITED ###
end

private
#使用回调共享操作之间的常见设置或约束。
def set_product
@product = Product.find(params [:id])
end

#不要信任吓人的互联网参数,只允许白名单通过。
def product_params
params.require(:product).permit(:title,:description,:price_usd,:price_isl,:image,:category_id,:stock_quantity,:label_id,:query,:slug)
end
end

我的模特产品类别有关系

product.rb

  class Product< ActiveRecord :: Base 
belongs_to:category
belongs_to:label
$ b has_many:product_items,:dependent => :destroy

扩展FriendlyId
friendly_id:title,use:[:slugged,:finders]

validates:title,:description,presence:true
验证:price_usd,:price_isl,数字性:{greater_than_or_equal_to:0.01}
validates:title,uniqueness:true
$ b has_attached_file:image,styles:{medium:500x500#,thumb: 100x100#}
validates_attachment_content_type:image,content_type:/\Aimage\/.*\/
end

category.rb

  class Category< ActiveRecord :: Base 

has_many:products,:dependent => :取消

扩展FriendlyId
friendly_id:name,use:[:slugged,:finders]
end

解决方案

我们来分析一下:

   




  • @products 好吧,大概是产品集合 code $> c code> Category.joins(:products) uhhh,这将返回类别的实例。 code>

  • 其中(产品:{id:@ product.image})匹配 @ product.image ,这表面上是一个字符串url或者文件路径


所有在一起: @products = 一个包含相关产品的类别数组,其中数字ID与特定产品上的特定字符串匹配



我对你的建议是复习Rails指南,特别关注 ActiveRecord基础知识

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

解决方案

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.

这篇关于无法在Ruby on Rails应用程序中显示产品图像下方的随机图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆