Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 Custom Class Question
    primarykey
    data
    text
    <p>I am trying to create a separate model class for image uploading, which was previously in the object's controller. I also want to make it agnostic, so I can upload images using one class from multiple objects.</p> <p>In my original object's model class, I have the following now:</p> <pre><code>class Object &lt; ActiveRecord::Base after_save :photo_store delegate :has_photo?, :photo, :photo_path, :store_photo, :photo_filename, :to =&gt; :photo_store def photo_store PhotoStore.new(self) end end </code></pre> <p>Then, the PhotoStore class looks like this:</p> <pre><code>class PhotoStore attr_reader :object def initialize(object) @object = object end def photo=(file_data) unless file_data.blank? @file_data = file_data self.extension = file_data.original_filename.split('.').last.downcase end end PHOTO_STORE = File.join RAILS_ROOT, 'public', 'photo_store' def photo_filename File.join PHOTO_STORE, "#{id}.#{extension}" end def photo_path "/photo_store/#{id}.#{extension}" end def has_photo? File.exists? photo_filename end private def store_photo if @file_data FileUtils.mkdir_p PHOTO_STORE File.open(photo_filename, 'wb') do |f| f.write(@file_data.read) end end end end </code></pre> <p>However, this throws the error below when I try and use the has_photo? method in the object's view.</p> <blockquote> <p>undefined local variable or method `id' for #</p> </blockquote> <p>Do I need to put some other type of relationship in place between the Object and PhotoStore?</p> <p>And a separate question: What's the best way to make this agnostic? Since it uses just the ID of the object, I could just include the Object's name in the filename, but is that the best way to do it?</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload