rails paperclip unable to access image from another view

Posted by curiousCoder on Stack Overflow See other posts from Stack Overflow or by curiousCoder
Published on 2013-11-03T11:21:07Z Indexed on 2013/11/03 15:53 UTC
Read the original article Hit count: 455

my app has an habtm relation b/w listings and categories. 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

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. note the plural @categories in show

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.

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. where's the issue? what am i missing?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby