0

我想删除这个逻辑:

Suitcase::Hotel.find(id: hotel.id).images.first.url

从视图。

https://gist.github.com/2719479

我没有样板酒店。我使用 Suitcase gem 通过 API 获取此 url。

问题是因为 只有在这样做的情况下才hotel来自@hotels = Suitcase::Hotel.find(location: "%#{headed}%")API 接收我的图像Suitcase::Hotel.find(id: hotel.id)

4

3 回答 3

1

If Suitcase::Hotel.find(id: hotel.id).images.first.url works then i would guess hotel.images.first.url will work too if hotel is an hotel instance.

于 2012-05-17T15:38:57.387 回答
1

正在添加:

@hotel = Suitcase::Hotel.find(id: hotel.id)

行动#show不起作用?

编辑:
在这种情况下,做一个帮手:

def hotel_image_url(hotel)
  Suitcase::Hotel.find(id: hotel.id).images.first.url
end

但正如我在这里看到的,您可以简单地在控制器中编写:

@hotels_data = Suitcase::Hotel.find(ids: @hotels.map(&:id))

或者更优雅地添加到您的模型(或创建装饰器(这是更好的选择)):

def photo
  Suitcase::Hotel.find(id: self.id).images.first.url
end
于 2012-05-17T15:36:59.017 回答
1

我认为这应该可行,不确定第二个选项

class Search < ActiveRecord::Base

  attr_accessible :headed, :children, :localization, :arriving_date, :leaving_date, :rooms, :adults
  def hotels
    @hotels ||= find_hotels
  end

private

  def find_hotels
    return unless headed.present?
    @hotels = Suitcase::Hotel.find(location: "%#{headed}%")
    @hotels.each do |hotel|
      def hotel.image_url
        Suitcase::Hotel.find(id: hotel.id).images.first.url
      end
    end
  end
end



# or this, but I'm not sure if this works

@hotels.each do |hotel|
  image_url =  Suitcase::Hotel.find(id: hotel.id).images.first.url
  def hotel.image_url
    image_url
  end
end
于 2012-05-17T16:18:42.467 回答