Paperclip不允许上传多张图片

时间:2012-09-01 18:46:52

标签: ruby-on-rails ruby ruby-on-rails-3 paperclip simple-form

以下是成功上传图片的日志:

Started POST "/kampagners" for 127.0.0.1 at 2012-09-01 20:18:01 +0000
Processing by KampagnersController#create as HTML
  Parameters: {"utf8"=>"V", "authenticity_token"=>"84w29N5okGI8BcL1cMc53fQzcVRAb
UTN/jssk6MI3qM=", "kampagner"=>{"titel"=>"lp", "landing_page"=>"lp", "kampagne_b
eskriv"=>"lp", "kampagne_maal"=>"l", "t1"=>"pl", "t2"=>"pll", "t3"=>"pl", "t4"=>
"p", "t5"=>"lp", "photos_attributes"=>{"photo"=>{"image"=>#<ActionDispatch::Http
::UploadedFile:0x38b5408 @original_filename="Skemaweb.PNG", @content_type="image
/png", @headers="Content-Disposition: form-data; name=\"kampagner[photos_attribu
tes][photo][image]\"; filename=\"Skemaweb.PNG\"\r\nContent-Type: image/png\r\n",
 @tempfile=#<File:C:/Users/AppData/Local/Temp/RackMultipart20120901-455
6-1d2qq7z>>}}, "ppc"=>"1", "soc"=>"1", "email"=>"1", "cashback"=>"0", "budget"=>
"12", "prisklik"=>"12"}, "commit"=>"Opret konkurrence"}
  ←[1m←[36m (18.0ms)←[0m  ←[1mBEGIN←[0m
  ←[1m←[35mSQL (0.0ms)←[0m  INSERT INTO `kampagners` (`budget`, `cashback`, `cli
cks`, `email`, `kampagne_beskriv`, `kampagne_maal`, `landing_page`, `ppc`, `pris
klik`, `soc`, `t1`, `t2`, `t3`, `t4`, `t5`, `titel`) VALUES (12, 0, 0, 1, 'lp',
'l', 'lp', 1, 12, 1, 'pl', 'pll', 'pl', 'p', 'lp', 'lp')
  ←[1m←[36mSQL (0.0ms)←[0m  ←[1mINSERT INTO `photos` (`image_content_type`, `ima
ge_file_name`, `image_file_size`, `image_remote_url`, `image_updated_at`, `kampa
gner_id`, `str`) VALUES ('image/png', 'Skemaweb.PNG', 114875, NULL, '2012-09-01
20:18:03', 39, NULL)←[0m
[paperclip] Saving attachments.
  ←[1m←[35m (2.0ms)←[0m  COMMIT

以上是上传2张图片,但没有成功:

Started POST "/kampagners" for 127.0.0.1 at 2012-09-01 20:19:03 +0000
Processing by KampagnersController#create as HTML
  Parameters: {"utf8"=>"V", "authenticity_token"=>"84w29N5okGI8BcL1cMc53fQzcVRAb
UTN/jssk6MI3qM=", "kampagner"=>{"titel"=>"lpl", "landing_page"=>"lp", "kampagne_
beskriv"=>"lp", "kampagne_maal"=>"lp", "t1"=>"llp", "t2"=>"lp", "t3"=>"l", "t4"=
>"pl", "t5"=>"lp", "photos_attributes"=>{"0"=>{"photo"=>{"image"=>#<ActionDispat
ch::Http::UploadedFile:0x690480 @original_filename="Skemaweb.PNG", @content_type
="image/png", @headers="Content-Disposition: form-data; name=\"kampagner[photos_
attributes][0][photo][image]\"; filename=\"Skemaweb.PNG\"\r\nContent-Type: image
/png\r\n", @tempfile=#<File:C:/Users/AppData/Local/Temp/RackMultipart20
120901-4556-1dcutiv>>}}, "1"=>{"photo"=>{"image"=>#<ActionDispatch::Http::Upload
edFile:0x694ba8 @original_filename="Skemaweb.PNG", @content_type="image/png", @h
eaders="Content-Disposition: form-data; name=\"kampagner[photos_attributes][1][p
hoto][image]\"; filename=\"Skemaweb.PNG\"\r\nContent-Type: image/png\r\n", @temp
file=#<File:C:/Users/Niceberg/AppData/Local/Temp/RackMultipart20120901-4556-t0dd
md>>}}}, "ppc"=>"1", "soc"=>"1", "email"=>"1", "cashback"=>"0", "budget"=>"21",
"prisklik"=>"23"}, "commit"=>"Opret konkurrence"}
  ←[1m←[35m (0.0ms)←[0m  BEGIN
  ←[1m←[36mSQL (1.0ms)←[0m  ←[1mINSERT INTO `kampagners` (`budget`, `cashback`,
`clicks`, `email`, `kampagne_beskriv`, `kampagne_maal`, `landing_page`, `ppc`, `
prisklik`, `soc`, `t1`, `t2`, `t3`, `t4`, `t5`, `titel`) VALUES (21, 0, 0, 1, 'l
p', 'lp', 'lp', 1, 23, 1, 'llp', 'lp', 'l', 'pl', 'lp', 'lpl')←[0m
  ←[1m←[35mSQL (0.0ms)←[0m  INSERT INTO `photos` (`image_content_type`, `image_f
ile_name`, `image_file_size`, `image_remote_url`, `image_updated_at`, `kampagner
_id`, `str`) VALUES (NULL, NULL, NULL, NULL, NULL, 40, NULL)
[paperclip] Saving attachments.
  ←[1m←[36mSQL (0.0ms)←[0m  ←[1mINSERT INTO `photos` (`image_content_type`, `ima
ge_file_name`, `image_file_size`, `image_remote_url`, `image_updated_at`, `kampa
gner_id`, `str`) VALUES (NULL, NULL, NULL, NULL, NULL, 40, NULL)←[0m
[paperclip] Saving attachments.
  ←[1m←[35m (1.0ms)←[0m  COMMIT
Redirected to

我的嵌套表格:

 <%= f.simple_fields_for :photos do |build| %>
   <%= render "billeder", :build => build %>
<% end %>

和_billeder:

<%= build.simple_fields_for :photo do |d| %>
    <%= d.file_field :image, :label => 'Image', :style => 'margin-bottom:2px;float:left;width:250px;'  %>
<% end %>

图片未上传的原因是什么?它们使用NULL NULL和NULL值创建。

更新:

控制器代码kampagners:

def new
    @kampagner = Kampagner.new
    2.times {@kampagner.photos.build }
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @kampagner }
    end
  end

  def create
    @kampagner = Kampagner.new(params[:kampagner])
    respond_to do |format|
      if @kampagner.save
        format.html { redirect_to(:kampagners, :notice => 'Kampagner was successfully created.') }
      else
        format.html { render :action => "new" }
      end
    end
  end

Kampagner模特:

  class Kampagner < ActiveRecord::Base
    has_many :photos
    accepts_nested_attributes_for :photos
    attr_accessible :titel, :budget, :photos_attributes, :ppc, :sem, :soc, :cashback,
                    :landing_page, :budget, :prisklik, :t1, :t2, :t3, :t4, :t5,
                    :kampagne_beskriv, :kampagne_maal, :email, :image_url, :billed_navn, :photos, :image, :photo
    end

照片模型:

 require 'open-uri'
    class Photo < ActiveRecord::Base
      belongs_to :kampagner
      attr_accessor :image_url, :billed_navn, :photo
      #sadasdasas
      Paperclip.options[:command_path] = "/usr/local/bin"
      has_attached_file :image,
              :path => ':rails_root/public/images/ads/:id/:basename.:extension',
              :url  => "images/ads/:id/:basename.:extension"
     attr_accessible :photos_attributes, :image_url, :billed_navn, :photo, :image, :kampagner_id, :image_remote_url, :image_file_name, :image_content_type, :image_file_size, :image_updated_at, :str
    end

表格:

<%= simple_form_for @kampagner, :html => { :multipart => true, :class => 'newform', :style => 'width:100%;' } do |f| %>
<h1 style="margin-top:10px;padding-top:0px;font-size:12px;">Tilføj billeder</h1>
 <%= f.simple_fields_for :photos do |build| %>
   <%= render "billeder", :build => build %>
<% end %>
<%= f.submit "Opret kampagne", :style => "margin-bottom:20px;" %>
<% end %>

1 个答案:

答案 0 :(得分:1)

看起来所需的参数应该没有额外的照片层,如:

"photos_attributes"=>{"0"=>{"image"=>#<ActionDispat
ch::Http::UploadedFile:0x690480 @original_filename="Skemaweb.PNG", @content_type
="image/png", @headers="Content-Disposition: form-data; name=\"kampagner[photos_
attributes][0][image]\";

它适用于单个附件案例,因为params已经采用所需的格式--- photo中的photos_attributes"=>{"photo"=>{"image"键 可能是一些其他标识符,如0,但它的价值(从{"image"}开始)是重要的。

编辑:(更正错误)

因此,请使用以下内容替换<%= f.simple_fields_for :photo ...部分中的_billeder块:

   <%= build.file_field :image, :label => 'Image', :style => 'margin-bottom:2px;float:left;width:250px;'  %>