Ruby on rails Rails回形针文件上载字段不工作-多选图像上载

Ruby on rails Rails回形针文件上载字段不工作-多选图像上载,ruby-on-rails,ruby,ruby-on-rails-3,paperclip,Ruby On Rails,Ruby,Ruby On Rails 3,Paperclip,我在上载文件上载字段中的图像时遇到一些问题。它不上传任何东西。但是图像url字段可以工作。我想这和我的模特有些关系 我的表格: <%= simple_form_for [:admin, @virksomhed] do |f| %> <%= f.simple_fields_for :link_attributes do |d| %> <% end %> <%= f.simple_fields_for :photo_attributes do

我在上载文件上载字段中的图像时遇到一些问题。它不上传任何东西。但是图像url字段可以工作。我想这和我的模特有些关系

我的表格:

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

“上载徽标”,:必需=>false%>
'Image,:required=>false',:style=>'页边距底部:2px'>
'计费URL',:必需=>false%>
我的照片模型:

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
需要“打开uri”
类照片“/public/images/biller/photo/:id/:basename.:extension”,
:path=>“:rails\u root/public/images/:id/:basename.:extension”
验证前:下载远程图像,:if=>:图像\u url\u是否提供?
私有的
是否提供def图像_url_?
!self.image\u url.blank?
终止
def下载远程图像
self.image=do\u下载\u远程\u映像
self.image\u remote\u url=image\u url
终止
def do_下载远程图像
io=open(URI.parse(image\uURL))
def io.original_文件名;base_uri.path.split('/').last;终止
io.original_filename.blank?零:io
rescue#使用验证而不是异常捕获url错误(Errno::enoint、OpenURI::HTTPError等)
终止
终止
我的virksomhed模型:

class Virksomhed < ActiveRecord::Base
has_one :photo

accepts_nested_attributes_for :photo
end
类Virksomhed尽管在后台使用了photo_属性,但我能找到的所有示例似乎都建议您使用以下内容:

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

{:multipart=>true}do | d |%>
对于我的表单,我添加了
,:html=>{:multipart=>true}


它解决了这个问题

本地和远程文件都失败了吗?编辑!:只有远程文件可以工作(与图像url字段一起),我认为它确实与Valdiation有关,然后输入字段消失。
<%= f.simple_fields_for :photo_attributes, :html => { :multipart => true } do |d| %>