[英]How do I get the data for a selected file from one form to another form in ruby on rails?
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. 现在,当用户通过微博表格选择照片后,我使用html5文件读取器api读取用户选择的本地图像,然后将其显示在页面下方的页面上。 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. 我像这样访问我的内容@ microposts.each做| m | ..... m.content以便可以访问像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. 我需要一些如何将刚刚存储在“微博专辑”相册中的图像的路径获取到微博表的图像列中,以便可以使用image_tag帮助器进行显示。
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. 因此,总而言之,我希望用户从微博表格中选择的照片上传到“微博相册”,这意味着将照片详细信息上传到照片表,并从photo_albums表中引用(如果有的话)。
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 亲切的问候
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? 还是facebook状态更新?
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. 另外,如果那行不通,我只需要在用户的PhotoAlbum中创建一张Photo并保存ID或路径或Micropost模型中的内容,那么您可以执行以下操作: image_tag User.first.microposts.last.image_path
是伪代码,但您可以理解。如果一个人要保存多个图像,则可以进行图像上载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.