2011-11-07 105 views
0

我有上傳圖片與我的文件上傳字段的一些問題。它不會上傳任何東西。但圖像網址字段的作品。我認爲這與我的模型有關。Rails回形針文件上傳字段不工作 - 多選圖片上傳

我的形式:

<%= 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 %> 

我的照片模式:

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 

我virksomhed型號:

class Virksomhed < ActiveRecord::Base 
has_one :photo 

accepts_nested_attributes_for :photo 
end 
+0

這對於本地和遠程文件都會失敗嗎? – Nick

+0

編輯!:只有遠程文件有效(帶有圖片url字段)我認爲它的確有一些關於值 –

回答

0
<%= f.simple_fields_for :photo_attributes, :html => { :multipart => true } do |d| %> 

,也爲我的形式,我添加, :html => { :multipart => true }

它解決了這個問題。

0

雖然photo_attributes在後臺使用,所有的例子我能找到似乎建議你應該使用這個:

<%= f.simple_fields_for :photo do |d| %> 
+1

然後輸入字段消失。 –