简体   繁体   English

使用输入类型文本在表单中上传图像

[英]Image upload in form using input type text

I'm kind of new to web designing. 我对网页设计有点新意。 I'm making a project on ruby on rails.I want a form for particular listings in which i can upload images for that listings.Images is in other table.Now one way is i can use file upload but when i edit any listings,it will show file field empty.So, i'm thinking to use input type text and associate a browse button and javascript for uploading image.Besides,this i also want javacsript function for adding new image,editing image(particular) or deleting image(particular).So,as u see its kind of giving pain in brain.please explain what to do easily and step wise.Thank You 我正在铁轨上制作一个关于ruby的项目。我想要一个特定列表的表单,我可以上传该列表的图像。图像在其他表中。现在一种方式是我可以使用文件上传但是当我编辑任何列表时,它会显示文件字段为空。所以,我正在考虑使用输入类型文本并将浏览按钮和javascript关联起来上传图像。此外,我还想要javacsript函数来添加新图像,编辑图像(特定)或删除图像(特别)。所以,正如你看到它在大脑中给予的痛苦。请解释一下容易做什么,一步一步明智。谢谢你

A text field cannot mimic an input type file the way you describe it. 文本字段无法以您描述的方式模仿输入类型文件。

You may also wanna take a look at this post about image upload with HTML, rails and paperclip How to POST files from HTML5 Drag-Drop to a Rails 3 App & Paperclip? 您可能还想看看这篇关于使用HTML,rails和paperclip上传图片的文章如何将文件从HTML5 Drag-Drop发布到Rails 3 App和Paperclip?

The awesome file attachment ruby gem Paperclip will do that for you. 令人敬畏的文件附件ruby gem Paperclip将为您做到这一点。

If you are new to these I strongly suggest watching these two screencasts from Railscasts: 如果您不熟悉这些内容,我强烈建议您观看Railscasts中的这两个截屏视频:

1) http://railscasts.com/episodes/196-nested-model-form-part-1 1) http://railscasts.com/episodes/196-nested-model-form-part-1

2) http://railscasts.com/episodes/134-paperclip 2) http://railscasts.com/episodes/134-paperclip

Image delete 图片删除

Delete an image with Paperclip 使用Paperclip删除图像

Image Upload 图片上传

The form add the actual form fields that will be used to upload the images using fields_for . 表单添加将用于使用fields_for上传图像的实际表单字段。 Also edit the form_for call and mark the form as multipart so it can accept file uploads. 还要编辑form_for调用并将表单标记为multipart,以便它可以接受文件上载。

<% form_for @user, :html => {:multipart => true} do |f| %>

…

<% f.fields_for :user_images do |builder| %>

<% if builder.object.new_record? %>

<%= builder.label :caption, "Image Caption" %>
<%= builder.text_field :caption %>

<%= builder.label :photo, "Image File" %>
<%= builder.file_field :photo %>

<% end %>

<% end %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM