Jump to content

The ultimate community for Ruby on Rails developers.


Photo

rails paperclip unable to access image from another view

ruby ruby on rails paperclip

  • Please log in to reply
3 replies to this topic

#1 arjunm009

arjunm009

    Signalman

  • Members
  • 12 posts
  • Locationindia

Posted 03 November 2013 - 09:17 AM

hey guys

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



#2 james

james

    Guard

  • Moderators
  • 221 posts
  • LocationLeeds, U.K.

Posted 03 November 2013 - 03:58 PM

Just a quick guess but it looks like your SQL is not pulling back the placeholder data.


Programming is just about problem solving!


#3 animesh

animesh

    Passenger

  • Members
  • 1 posts
  • LocationBhubaneswar

Posted 05 November 2013 - 12:43 PM

Don't use find_by_sql in this scenario as once it is used you won't get any reference of 'placeholder' in the 'category' object.

So use 'Find' with 'Join','SELECT' etc.


  • arjunm009 likes this

#4 arjunm009

arjunm009

    Signalman

  • Members
  • 12 posts
  • Locationindia

Posted 12 November 2013 - 05:32 PM   Best Answer

i have come to learn, paperclip works with activerecord to pull data from the db. so doing a sql on other model wch are in habtm, it wont be able to access paperclip's methods of accessing db.

 

but yes there are ways as pointed out.

 

first

@category = Category.find([params[:c][:id1],params[:c][:id2]])

and then calling respective columns of other model from the view.

 

second

@category = Listing.includes(:categories).find([params[:c][:id1],params[:c][:id2]])

calling directly the model which hosts the paperclip columns in the first place, makes it work.

 

third

@categories = Listing.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]]

sql can be applied if you call the model hosting the paperclip column.

 

 

in short, call the model hosting the paperclip in any controller to do your parsing.







Also tagged with one or more of these keywords: ruby, ruby on rails, paperclip

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users