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