[英]Rails paperclip file upload field not working - multiple choice image upload
I am having some problem with uploading images with my file upload field. 我在使用文件上传字段上传图像时遇到问题。 It does not upload anything.
它不上传任何东西。 But the image url field works.
但是图片网址字段有效。 I think it have some to do with my model.
我认为这与我的模型有关。
My form: 我的表格:
<%= simple_form_for [:admin, @virksomhed] do |f| %>
<%= f.simple_fields_for :link_attributes do |d| %>
<% end %>
<%= f.simple_fields_for :photo_attributes do |d| %>
<%= d.label :image, :label => 'Upload logo', :required => false %>
<%= d.file_field :image, :label => 'Image, :required => false', :style => 'margin-bottom:2px' %>
<%= d.input :image_url, :label => 'Billed URL', :required => false %>
<% end %>
<%= f.submit "Opret virksomhed" %>
<% end %>
My Photo model: 我的照片模型:
require 'open-uri'
class Photo < ActiveRecord::Base
belongs_to :virksomhed
attr_accessor :image_url
has_attached_file :image,
:url => "/public/images/billeder/photo/:id/:basename.:extension",
:path => ":rails_root/public/images/:id/:basename.:extension"
before_validation :download_remote_image, :if => :image_url_provided?
private
def image_url_provided?
!self.image_url.blank?
end
def download_remote_image
self.image = do_download_remote_image
self.image_remote_url = image_url
end
def do_download_remote_image
io = open(URI.parse(image_url))
def io.original_filename; base_uri.path.split('/').last; end
io.original_filename.blank? ? nil : io
rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
end
end
My virksomhed model: 我的虚拟模型:
class Virksomhed < ActiveRecord::Base
has_one :photo
accepts_nested_attributes_for :photo
end
尽管在后台使用了photo_attributes,但我可以找到的所有示例似乎都建议您使用此方法:
<%= f.simple_fields_for :photo do |d| %>
<%= f.simple_fields_for :photo_attributes, :html => { :multipart => true } do |d| %>
And also for my form I added , :html => { :multipart => true }
对于我的表单,我还添加了
, :html => { :multipart => true }
It solved the problem. 它解决了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.