显示与所选产品属于同一类别的随机商品图片.ROR app

时间:2017-08-04 15:16:41

标签: ruby-on-rails ruby foreign-keys many-to-many paperclip

我正在修改我一直在努力的Rails应用程序。

我安装了一个模型images.rb来处理产品的所有图片。

当用户选择了产品时,用户会在app/views/products/show.html.erb

看到该产品

show.html.erb的底部,应用程序向用户提供与用户正在查看的产品属于同一类别的类似产品的建议。

在我添加image.rb模型来处理图像之前,每个产品都附有一张图片并且每件事都有效,但现在我收到了错误。以下是我用于显示同一类别中随机产品的代码:

show.html.erb

 <div class="row 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 %>
                <%= 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>

products_controller.rb show方法中,我使用此变量来获取同一类别中的随机产品。

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

  def show    
     @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_usd,  :image, :category_id, :stock_quantity, :label_id, :query, :slug, images_attributes: [:image , :id , :_destroy])
  end
 end

我添加image.rb后,在尝试加载展示页面时,我总是收到此错误:undefined method 'url' for "":String。错误出现在以下行中:<%= image_tag @product.image.url, :size => "100%x100%", class: "img-responsive center-block" %>

product.rb模型

class Product < ActiveRecord::Base
  acts_as_list :scope => [:category, :label]
  belongs_to :category
  belongs_to :label

  has_many :images
  accepts_nested_attributes_for :images
  has_many :product_items, :dependent => :destroy

    validates :title, :description, presence: true
    validates :price_usd, :price_isl, numericality: {greater_than_or_equal_to: 0.01}
    validates :title, uniqueness: true  
 end

image.rb模型

class Image < ActiveRecord::Base
  belongs_to :product
    has_attached_file :image, styles: { medium: "500x500#", thumb: "100x100#" }
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
 end

我不知道该怎么做,有人可以告诉我吗?

1 个答案:

答案 0 :(得分:3)

如果您的Product has_many :images然后@product.image.url无效。

@product.images.sample会为您提供该产品的随机图片。

看起来您正在使用Paperclip,您已将其配置为向image模型添加Image附件,因此您需要为您添加额外的方法链:

@product.images.sample.image.url将检索随机选择的image关联的图片附件的网址。

@product.image目前返回一个空字符串,因此您必须在image productsstring类型的text表上添加image列,或者模型上的def change remove_column :products, :image end 方法。如果您不再需要它,则应将其删除。

如果它是表格中的一列,则在迁移文件中:

Image

修改

正如Mr.Yoshiji所建议的那样,在数据库级别获得随机图像会更有效,特别是如果产品有很多相关图像。 Product模型上的范围方法可以实现,并使用class Image < ActiveRecord::Base scope :random, -> { order("RANDOM()").limit(1) } end class Product < ActiveRecord::Base def random_image images.random.take end end 方法对其进行抽象并简化视图。

@product.random_image.image.url

然后在你看来

Join