简体   繁体   English

另一个视图的Rails回形针访问图像

[英]rails paperclip access image from another view

my app has an habtm relation b/w listings and categories. 我的应用程序有一个habtm关系黑白列表和类别。 now from the categories index page, a user filters select box to view listings in the show page. 现在,从类别索引页面中,用户可以过滤选择框以在显示页面中查看列表。

now i am not able to access images attached to listings in the category show page. 现在,我无法访问类别显示页面中附加到列表的图像。

listing.rb 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"

categories controller 类别控制器

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

the sql just filters and displays the listings in show page where i can show its attributes, but cant access the placeholder. sql只是过滤并在显示页面上显示列表,我可以在其中显示其属性,但无法访问占位符。 note the plural @categories in show 注意显示中的复数@categories

categories show page 分类显示页面

<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 从带有回形针宝石红宝石的视图中访问不同视图中的图像

this said to make the object plural and call a loop, wch shall allow to access the image. 这表示使对象复数并调用循环,wch应允许访问图像。

it does not work in this case. 在这种情况下不起作用。

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

but the amazing thing is, placeholder will be displayed as an array of all images for all the listings if used as suggested in that stackoverflow, wch is, obviously, not the way i prefer. 但是令人惊奇的是,如果按照该stackoverflow中的建议使用,占位符将显示为所有列表的所有图像的数组,显然,wch不是我喜欢的方式。 where's the issue 问题在哪里

Because category haven`t method placeholder, instead it have 因为类别没有方法占位符,所以它有

Update: 更新:

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

upd 更新

Try just get category and then loop through its lisings 尝试仅获取类别,然后遍历其类别

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: upd2:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM