rails paperclip从另一个视图访问图像

时间:2013-11-02 20:18:07

标签: ruby-on-rails ruby paperclip

我的应用与b / w列表和类别有habtm关系。现在,从类别索引页面,用户过滤选择框以在显示页面中查看列表。

现在我无法访问类别显示页面中附加到列表的图像。

listing.rb

attr_accessible :placeholder, :categories_ids

  has_and_belongs_to_many :categories
  has_attached_file :placeholder, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png",
                      :url => "/system/:hash.:extension",
                     :hash_secret => "longSecretString"

类别控制器

def index
  @categories = Category.all
end

def show
  @categories = Category.find_by_sql ["select distinct l.* from listings l , categories c, categories_listings cl where c.id = cl.category_id and l.id = cl.listing_id and c.id in (?,?)" ,  params[:c][:id1] , params[:c][:id2]]
end

sql只过滤并显示显示页面中的列表,我可以在其中显示其属性,但无法访问占位符。请注意show

中的复数 @categories

类别显示页面

<ul>
  <% @categories.each_with_index do |c, index| %>
  <% if  index == 0 %>
    <li class="first"><%= c.place %></li>

    <%= image_tag c.placeholder.url(:thumb) %>

    <li><%= c.price %></li>

    <% else %>
    <li><%= c.place %></li>
    <li><%= c.price %></li>

    <%= image_tag c.placeholder.url(:thumb) %>
    <% end %>

  <% end %>
</ul>

Access image from different view in a view with paperclip gem ruby on rails

这说要使对象复数并调用循环,wch将允许访问图像。

在这种情况下不起作用。

undefined method `placeholder' for #<Category:0x5c78640>

但令人惊奇的是,占位符将显示为所有列表的所有图像的数组,如果按照堆栈溢出中的建议使用,wch显然不是我喜欢的方式。问题在哪里

1 个答案:

答案 0 :(得分:0)

因为类别避免了方法占位符,而是

更新

@category.listings.each  do  |l| 
  l.placeholder
end

UPD

尝试获取类别,然后遍历其中的链接

def show 
  @category = Category.find(params[:id]) #you get category and you able to loop throgh it listing array and get it placeholder
end

upd

@categoies.each_with_index  do  |c| 
  c.listings.each do |l|
    l.placeholder
  end
end

UPD2:

@categoies.each_with_index  do  |c,i| 
  c.listings.each do |l|
    image_tag l.placeholder.url(:thumb)
  end
end