我正在修改我一直在开发的 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
我不知道该怎么办,有人可以建议我吗?
最佳答案
如果您的Product has_many :images
然后@product.image.url
是行不通的。
@product.images.sample
将为您提供该产品的随机图像。
您似乎正在使用 Paperclip
,您已将其配置为添加 image
附件到您的Image
模型,因此您需要向链中添加一个额外的方法:
@product.images.sample.image.url
将检索随机选择的图像附件的 URL image
协会。
@product.image
当前返回一个空字符串,因此您必须有一个 image
专栏上您的products
类型表string
或text
,或 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/