This question comes as a consequense of a previous one (Return url from paperclip to json). I am summing it up in order to be easier to follow and also because this is just a part of the above mentioned one. I have a CMS system that uses paperclip for multiple image uploading. My code is
asset.rb
attr_accessible :asset_content_type, :asset_file_name, :asset_file_size, :asset_updated_at, :place_id, :asset
belongs_to :place
has_attached_file :asset
validates_attachment :asset, :presence => true,
:content_type => { :content_type => ['image/jpeg', 'image/png'] },
:size => { :in => 0..1.megabytes }
def asset_url
asset.url(:original)
end
place.rb
has_many :assets
accepts_nested_attributes_for :assets, :allow_destroy => true
def avatar_url
asset_url
end
places_controller
def overall_photos
@places = Place.all
render :json => @places.to_json(:methods => [:avatar_url])
end
The error I get when I try to access ...places/overall_photos.json is {"status":"500","error":"undefined local variable or method `asset_url' for #\u003CPlace:0x007f163b67d8a8\u003E"} so it seems tat I can't access the instance method of asset.rb through the associate model place.rb. Can anyone point me to the right direction? I even tried to make asset_url a class method but still no luck.
def avatar_url
assets.map(&:asset_url)
end
This should give you what you want according to your comment