简体   繁体   English

Rails回形针无法从另一个视图访问图像

[英]rails paperclip unable to 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? 问题在哪里? what am i missing? 我想念什么?

Having looked at your code, the immediate issue I can see is that placeholder is part of the listing.rb model, not category.rb 看完您的代码后,我看到的直接问题是placeholderlisting.rb模型的一部分,而不是category.rb


ActiveRecord Objects ActiveRecord对象

When you call an ActiveRecord object (eg @categories = Category.all ), the ActiveRecord object is created on the Category model, which means any dependencies from that will have to be accessed like an array -> @categories.listings 当您调用ActiveRecord对象(例如@categories = Category.all )时,将在Category模型上创建ActiveRecord对象,这意味着必须像访问数组那样访问任何依赖项-> @categories.listings

The associations of these records are created with the various has_and_belongs_to_many associations you created in your models, which seem to be working correctly 这些记录的关联是使用您在模型中创建的各种has_and_belongs_to_many关联创建的,它们似乎正常工作

So my recommendation is to call listings as a dependency of Category, like this: 因此,我的建议是将列表称为Category的依赖项,如下所示:

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

Update 更新

To show all the images for the listings, you will have to just do an .each loop, like this: 要显示清单的所有图像,您只需要执行.each循环,如下所示:

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

        <% c.listings.each do |listing| %>
            <%= image_tag listing.placeholder.url(:thumb) %> 
        <% end %>

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

        <% c.listings.each do |listing| %>
            <%= image_tag listing.placeholder.url(:thumb) %> 
        <% end %>

    <% end %>

  <% end %>
</ul>

ActiveRecord & Paperclip ActiveRecord和回形针

I think the conclusion is this: 我认为结论是这样的:

Your SQL query is bringing back the "pure data" from your listings database. 您的SQL查询正在从列表数据库中带回“纯数据”。 That's fine, but you want to use Paperclip's .url function, which only fires with ActiveRecord (it creates an image object & attaches to the ActiveRecord object) 很好,但是您想使用Paperclip的.url函数,该函数仅与ActiveRecord触发(它创建图像对象并附加到ActiveRecord对象)

Your listings db has these columns: 您的列表数据库具有以下列:

  • placeholder_file_name placeholder_file_name
  • placeholder_content_type placeholder_content_type
  • placeholder_file_size placeholder_file_size
  • placeholder_updated_at placeholder_updated_at

These cannot create an image output for you, as the image file is stored in a different path to the placeholder_file_name 这些无法为您创建图像输出,因为图像文件存储在placeholder_file_name的不同路径中

When you use Paperclip, it takes the data stored in these columns & creates an ActiveRecord image object, which you can then call by using c.listings.first.placeholder.url . 使用Paperclip时,它将使用存储在这些列中的数据并创建一个ActiveRecord图像对象,然后可以使用c.listings.first.placeholder.url进行调用。 Notice how the placeholder column is not in the database at all; 请注意, placeholder列完全不在数据库中。 meaning Paperclip needs to be called by ActiveRecord before you can use its functionality 意味着需要使用ActiveRecord才能调用Paperclip,然后才能使用它的功能

You could use a query like this (this is a complete stab in the dark to be honest, but hope it gives you more insight): 可以使用这样的查询(老实说,这是一个黑暗的完整提示,但希望它能为您提供更多的见识):

@categories = Category.joins(:listings).where(:id => [params[:c][:id1],params[:c][:id2]])

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

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