2012-09-01 59 views
0

下面是从日志成功1张图片上传:回形针不需额外允许上传多张图片

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 

下面是我上传4张图片,但没有更迭:

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创建的。

UPDATE:

控制器代码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 %> 
+0

请发表您的控制器相关的方法为好。 – prusswan

+0

我已更新我的问题。 –

+0

还有'创造'行动 – prusswan

回答

1

它看起来像期望的PARAMS应该出现无照片的附加层,如:

"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已经在所需的格式--- photos_attributes"=>{"photo"=>{"image" 中的photo密钥可能是其他一些标识符,如0,但是它的值(从{"image"}开始)非常重要。

编辑:(纠正错误)

因此,只有以下取代你<%= f.simple_fields_for :photo ...块在_billeder部分:

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

我已经使用你的解决方案。参数是一样的,但这次它的工作原理很奇怪。 –

+0

如何编辑时,不会出现图像.. –