2013-04-07 57 views
1

有誰知道我如何從elasticsearcg(使用輪胎)索引的圖像獲取image.url?如何從索引的ElasticSearch模型獲取image.url

class Photo < ActiveRecord::Base 
    include Tire::Model::Search 
    include Tire::Model::Callbacks 
    attr_accessible :caption, :user_id, :image, :venue_id 
    attr_accessible :id, :image_file_name, :created_at, :updated_at, :city, :state, :country, :image_url 
    has_attached_file :image, :styles => { :large => "9999x9999", :medium => "300x200>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png" 
end 

class SearchController < ApplicationController 
    def index 
    city_name = params[:city] 
    @photos = Photo.search city_name 
     respond_to do |format| 
     format.html # index.html.erb 
    end 
end 

在我index.html.erb當我這樣做:

<% @photos.each do |photo| %> 
    <%= image_tag photo.image.url(:medium) %> 

我收到一個未定義的方法`網址」爲無:NilClass

任何人可以幫助我?

謝謝Ÿ提前

回答

0

的圖像包含在你的模型存儲在,所以你需要你的搜索功能來查詢ElasticSearch SQL數據庫非常多,然後查找相應的條目在該數據庫中。這通過傳遞load: truePhoto.search來完成。

@photos = Photo.search city_name, load: true

2

添加到您的照片模式:

mapping do 
    indexes :image 
end 

def image 
    picture.url(:medium) 
end 

後在你看來

<%= image_tag photo.image %>