Ruby on rails 显示与所选产品所在类别相同的随机产品图像。ROR应用程序
我正在修改我一直在使用的Rails应用程序 我安装了一个型号Ruby on rails 显示与所选产品所在类别相同的随机产品图像。ROR应用程序,ruby-on-rails,ruby,foreign-keys,many-to-many,paperclip,Ruby On Rails,Ruby,Foreign Keys,Many To Many,Paperclip,我正在修改我一直在使用的Rails应用程序 我安装了一个型号images.rb来处理产品的所有图像 当用户选择了一个产品时,用户会在app/views/products/show.html.erb <div class="row product-teaser"> <h4 class="text-center teaser-text"> similar products to <%= @product.title %> : </h4> &
images.rb
来处理产品的所有图像
当用户选择了一个产品时,用户会在app/views/products/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>
在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>
添加image.rb
后,我总是会遇到以下错误:尝试加载显示页面时,未定义“”的方法“url”:String
。错误出现在此行中:“100%x100%”,类别:“img响应中心块”%>
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
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.image.url
就不起作用了
@product.images.sample
将为您提供该产品的随机图像
看起来您正在使用回形针
,您已将其配置为将图像
附件添加到图像
模型,因此您需要向链添加额外的方法:
@product.images.sample.image.url
将检索随机选择的image
关联的图像附件的url
@product.image
当前返回一个空字符串,因此您的产品
表上必须有image
列,类型为字符串
或文本
,或者模型上的image
方法。如果你不再需要它,那么你应该移除它
如果它是表中的一列,则在迁移文件中:
def change
remove_column :products, :image
end
编辑
正如Yoshiji先生所建议的那样,在数据库级别获取随机图像将更有效,特别是当一个产品有大量关联图像时。图像
模型上的scope方法可以实现这一点,同时使用产品
方法来抽象它并简化视图
class Image < ActiveRecord::Base
scope :random, -> { order("RANDOM()").limit(1) }
end
class Product < ActiveRecord::Base
def random_image
images.random.take
end
end
谢谢你的回答@m。simon borg,在
show.htlm.erb
的另一部分,我正在使用这个代码,它是有效的:
谢谢,更新了答案,因为我知道它应该是有很多@daudihell感谢更新,我明白你的意思,但是,现在我得到了这个错误:未定义的方法
url'for#`更新了答案。看到您正在配置曲别针,以便将图像
附件添加到图像
模型。因此,要获取图像的URL,它应该是image.image.URL
,而不是image.URL
@daudihell。可以在数据库级别执行随机逻辑,而不是调用数组#sample
,这将节省一些性能(取决于产品
具有图像
的数量)
@product.random_image.image.url