0
votes

I have a micropost form which makes it possible for users to post microposts. Now I'd like to give them the ability to upload photo/photos to represent microposts they'll post.

Here is the form:

   = form_for @micropost, :remote => true do |f|
        = f.file_field :image  # Needed for image previewing with html5 file api
        = f.hidden_field :user_id
        = f.text_area :content
        = f.submit "Post"

I already have already setup models, database tables for creating photo albums and uploading photos to them.

I have a photo model:

class Photo < ActiveRecord::Base

    belongs_to :photo_album

    attr_accessible :photo_album_id, :photo_title, :image, :remote_image_url
    mount_uploader :image, ImageUploader


    alpha_num_non_word_char = /^[a-zA-Z0-9\D_ -]*$/

    validates :image, :presence => true
    validates :photo_title, :presence => true,
      :length        => { :minimum => 2, :maximum => 50 },
      :format        => {
                          :with => alpha_num_non_word_char,
                          :message => "error"
                        }    
    validate :picture_size_validation, :if => "image?"

    def picture_size_validation
    errors[:image] << "Your photo should be less than 1MB" if image.size > 1.megabytes
    end

end

and a photo model:

class PhotoAlbum < ActiveRecord::Base

    belongs_to :user
    has_many :photos
    belongs_to :photo

    attr_accessible :album_title, :user_id, :photo_id


    alpha_num_non_word_char = /^[a-zA-Z0-9\D_ -]*$/

    validates :album_title, :presence => true,
      :length        => { :minimum => 2, :maximum => 50 },
      :format        => {
                          :with => alpha_num_non_word_char,
                          :message => "error"
                        }    
end

Upon user registration a "microposts album" is created for them and this will hold all the photos they upload for microposts.

I could easily get access to the microposts album like so:

@photo = Photo.new(:photo_album_id => PhotoAlbum.where(:user_id => current_user.id, :album_title => "microposts album").first.id)

Finally here is the form for upload photos to photo albums:

= form_for @photo, :html => {:multipart => true} do |f|
  = f.hidden_field :photo_album_id
  = f.label :photo_title
  = f.text_field :photo_title
  = f.file_field :image
  = f.submit

What I'd like a solution/help with:

Right now after a user selects a photo via the microposts form I use html5 file reader api to read the users selected local image and then display it on the page under the page. This is good but what I want to happen next is for the selected image to be uploaded and added to the "microposts album". I would then like to display it with the content of the micropost.

I access my content like this @microposts.each do |m|..... m.content so I could access the image like m.image. Some how I would need to get the path of the image that just got stored in the "microposts album" album into my image column of the microposts table so I could display it using an image_tag helper.

So to sum it all up I want for users selected photo from the microposts form to be uploaded to "microposts album" meaning the photo details are uploaded to the photos table and referenced from the photo_albums table if that makes sense at all.

I'm not sure if I have thought this out back to front but I'd like to get some help, ideas or solutions if possible.

I just thought it would be better to use the existing image upload resources I have instead of creating a new one for microposts.

Thanks in advance

Kind regards

1
Are MicropostAlbum and PhotoAlbum the same thing? There are a lot of words, but i think you want one Photo uploader to display your various types of Albums, be it PhotoAlbum or MicropostAlbum or RecordCoverAlbum... or ....? If not, maybe you need to rejig what you are saying into a simple sentence or statement.pjammer
microposts album is the name of one of the albums stored in the album_title column of the photo_albums table. I need to make it so that any time a photo is uploaded with a micropost, it's image is stored in the microposts album (attribute for album_title column in photo_albums table). Does this make sense? Tried to explain as best as I can. Let me know thanksLondonGuy

1 Answers

1
votes

Cobbling this together, and speaking from a Paperclip point of view, why don't you just have paperclip handle all image uploads in a 'micropost' whatever that is. Is that like a tweet with an image in it? Or facebook status update?

Also, if that won't work, I'd just create a Photo in a user's PhotoAlbum and save the Id or path or something in the Micropost model, so you can do something like image_tag User.first.microposts.last.image_path this is pseudo code, but you get the idea.If a guy has more than one image to save, then you do the image uploader angle.