Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 显示与所选产品所在类别相同的随机产品图像。ROR应用程序_Ruby On Rails_Ruby_Foreign Keys_Many To Many_Paperclip - Fatal编程技术网

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

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> &

我正在修改我一直在使用的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>
   <% @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