显示与所选产品所在类别相同的随机产品图片。ROR 应用
Showing random Product images from same category as the selected product is in. ROR app
我正在修改我一直在开发的 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
将为随机选择的 image
关联的图像附件检索 URL。
@product.image
当前返回空字符串,因此您的 products
table 中必须有一个 string
或 [=] 类型的 image
列25=],或模型上的 image
方法。如果您不再需要它,则应将其删除。
如果它是您 table 中的一列,在迁移文件中:
def change
remove_column :products, :image
end
编辑
正如 Yoshiji 先生所建议的那样,在数据库级别可以更有效地获取随机图像,尤其是在产品具有大量关联图像的情况下。 Image
模型上的作用域方法可以解决这个问题,同时使用 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
我正在修改我一直在开发的 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
将为随机选择的 image
关联的图像附件检索 URL。
@product.image
当前返回空字符串,因此您的 products
table 中必须有一个 string
或 [=] 类型的 image
列25=],或模型上的 image
方法。如果您不再需要它,则应将其删除。
如果它是您 table 中的一列,在迁移文件中:
def change
remove_column :products, :image
end
编辑
正如 Yoshiji 先生所建议的那样,在数据库级别可以更有效地获取随机图像,尤其是在产品具有大量关联图像的情况下。 Image
模型上的作用域方法可以解决这个问题,同时使用 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