ruby-on-rails - 显示与所选产品所在类别相同的随机产品图像。ROR 应用程序

标签 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.rbshow方法我有这个变量来从同一类别中获取随机产品。

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

我不知道该怎么办,有人可以建议我吗?

最佳答案

如果您的Product has_many :images然后@product.image.url是行不通的。

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

您似乎正在使用 Paperclip ,您已将其配置为添加 image附件到您的Image模型,因此您需要向链中添加一个额外的方法:

@product.images.sample.image.url将检索随机选择的图像附件的 URL image协会。

@product.image当前返回一个空字符串,因此您必须有一个 image专栏上您的products类型表stringtext ,或 image方法在你的模型上。如果您不再需要它,则应该将其删除。

如果它是表中迁移文件中的一列:

def change
  remove_column :products, :image
end

编辑

正如 MrYoshiji 所建议的,在数据​​库级别获取随机图像会更有效,特别是如果产品有大量关联图像的话。 Image 上的范围方法模型可以做到这一点,以及 Product方法来抽象它并简化您的 View 。

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

关于ruby-on-rails - 显示与所选产品所在类别相同的随机产品图像。ROR 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45510324/

相关文章:

ruby-on-rails - rails : Wrong hostname for url helpers in rspec

ruby - 如何替换定义节点的标签?

mysql - 我们可以为一个外键引用两个不同的表吗?

ruby-on-rails - 应该返回什么葡萄救援_从:all block?

css - Assets :precompile throws error: undefined method `each' for true:TrueClass

java - 在 JTable 中为数据库外键添加组合框

sql - 外键约束的条件级联操作?

ruby-on-rails - 什么时候停止 DRYing 代码?

ruby-on-rails - 从远程 mp3 文件中检索 id3 信息

ruby-on-rails - “bundle install”在安装rspec时失败