1

我正在尝试在表单上上传多个文件。我遵循了回形针和嵌套属性的轨道铸件,以及本教程http://sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/但我似乎不能让它工作...

我也在堆栈溢出中搜索过这里,查看了所有回形针和嵌套属性帖子,但我似乎找不到我的答案,似乎我做的一切都是正确的......

发生的情况是,当我提交表单时,它会创建广告(它是一个广告应用程序),它说一切正常,但它不会将图像数据写入数据库,也不会上传文件.. .

所以我有分类模型:

class Classified < ActiveRecord::Base
has_many :classified_images, :dependent => :destroy

accepts_nested_attributes_for :classified_images, :reject_if => lambda { |t| t['classified_image'].blank? }

attr_accessible :classified_images_attributes, :access, :contact, :price, :bizType 
end

然后,Classified_Image 模型:

class ClassifiedImage < ActiveRecord::Base
belongs_to :classified
has_attached_file :photo, :styles => {:small => "150x150>", :large => "320x240>"},
  :url => "/assets/products/:id/:style/:basename.:extension",
  :path => ":rails_root/public/assets/classifieds/:id/:style/:basename.:extension"

validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes

attr_accessible :caption, :photo
end

在分类控制器上,在“新”部分,我有: def new @classified = Classified.new

3.times { @classified.classified_images.build }

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @classified }
end

end

在“_form”我有:

<%= form_for @classified, :html => { :multipart => true } do |f| %>
...
<%= f.fields_for :classified_images do |builder| %>
<%= render 'image_fields', :f => builder %>
<% end %>

在“image_fields”部分我有:

<% if f.object.new_record? %>
<li>
<%= f.label :caption %>
<%= f.text_field :caption %>
<%= f.label :photo %>
<%= f.file_field :photo %>
</li>
<% end %>

在我的迁移文件上:

class AddAttachmentPhotoToClassifiedImages < ActiveRecord::Migration
def self.up
  add_attachment :caption, :classified_id, :photo
end

def self.down
drop_attached_file :caption, :classified_id, :photo
end
end

class CreateClassifiedImages < ActiveRecord::Migration
def change
create_table :classified_images do |t|
  t.string :caption
  t.integer :classified_id

  t.timestamps
end
end
end

在“development.rb”文件中,我有:

 Paperclip.options[:command_path] = "/usr/local/bin/"
 Paperclip.options[:log] = true

这是我提交表单时的日志示例:

在 2013-05-19 23:39:43 +0100 开始 POST "/classifieds" for 127.0.0.1 由 ClassifiedsController#create 作为 HTML 参数处理:{"utf8"=>"✓", "authenticity_token"=>"978KGJSULmMEvr6Tysg5xYIEQzNLn5vod07g+ Z7njkU=", "classified"=>{"contact"=>"918218338", "price"=>"1500", "access"=>"bons", "classified_images_attributes"=>{"0"=>{" caption"=>"teste", "photo"=>#@original_filename="064_dont-count-the-days.jpg", @content_type="image/jpeg", >@headers="Content-Disposition: form-data ; name=\"classified[classified_images_attributes][0][photo]\"; filename=\"064_dont-count-the-days.jpg\"\r\nContent-Type:图片/jpeg\r\n", >@tempfile=#3954-11t04t>>}, "1"=>​​{"caption"=>""}, "2"=>{"caption"=>""} }}, "commit"=>"Criar novo >Classificado"} (0.1ms) 开始事务 SQL (0.5ms) INSERT INTO "classifieds" ("access", "contact", "created_at", "price",) >值 (?, ?, ?, ?) [["access", "bons"], ["contact", "918218338"], ["created_at", Sun, 19 > May 2013 22:39:43 UTC +00 :00], ["price", 1500], ["updated_at", Sun, 19 May 2013 22:39:43 UTC >+00:00]] (0.8ms) 提交事务重定向到 localhost:3000/classifieds/8完成 302 Found in 5ms (ActiveRecord: 1.4ms)>}, "1"=>​​{"caption"=>""}, "2"=>{"caption"=>""}}}, "commit"=>"Criar novo >Classificado"} (0.1ms ) 开始事务 SQL (0.5ms) INSERT INTO "classifieds" ("access", "contact", "created_at", "price",) >VALUES (?, ?, ?, ?) [["access", "bons "], ["contact", "918218338"], ["created_at", Sun, 19 >May 2013 22:39:43 UTC +00:00], ["price", 1500], ["updated_at", Sun , 2013 年 5 月 19 日 22:39:43 UTC >+00:00]] (0.8ms) 提交事务重定向到 localhost:3000/classifieds/8 完成 302 发现在 5ms (ActiveRecord: 1.4ms)>}, "1"=>​​{"caption"=>""}, "2"=>{"caption"=>""}}}, "commit"=>"Criar novo >Classificado"} (0.1ms ) 开始事务 SQL (0.5ms) INSERT INTO "classifieds" ("access", "contact", "created_at", "price",) >VALUES (?, ?, ?, ?) [["access", "bons "], ["contact", "918218338"], ["created_at", Sun, 19 >May 2013 22:39:43 UTC +00:00], ["price", 1500], ["updated_at", Sun , 2013 年 5 月 19 日 22:39:43 UTC >+00:00]] (0.8ms) 提交事务重定向到 localhost:3000/classifieds/8 完成 302 发现在 5ms (ActiveRecord: 1.4ms)caption"=>""}}}, "commit"=>"Criar novo >Classificado"} (0.1ms) 开始事务 SQL (0.5ms) INSERT INTO "classifieds" ("access", "contact", "created_at" , "price",) >VALUES (?, ?, ?, ?) [["access", "bons"], ["contact", "918218338"], ["created_at", Sun, 19 >May 2013 22 :39:43 UTC +00:00], ["price", 1500], ["updated_at", Sun, 19 May 2013 22:39:43 UTC >+00:00]] (0.8ms) 提交事务重定向到localhost:3000/classifieds/8 Completed 302 Found in 5ms (ActiveRecord: 1.4ms)caption"=>""}}}, "commit"=>"Criar novo >Classificado"} (0.1ms) 开始事务 SQL (0.5ms) INSERT INTO "classifieds" ("access", "contact", "created_at" , "price",) >VALUES (?, ?, ?, ?) [["access", "bons"], ["contact", "918218338"], ["created_at", Sun, 19 >May 2013 22 :39:43 UTC +00:00], ["price", 1500], ["updated_at", Sun, 19 May 2013 22:39:43 UTC >+00:00]] (0.8ms) 提交事务重定向到localhost:3000/classifieds/8 Completed 302 Found in 5ms (ActiveRecord: 1.4ms), "contact", "created_at", "price",) >VALUES (?, ?, ?, ?) [["access", "bons"], ["contact", "918218338"], ["created_at" , Sun, 19 >May 2013 22:39:43 UTC +00:00], ["price", 1500], ["updated_at", Sun, 2013 年 5 月 19 日 22:39:43 UTC >+00:00]] (0.8ms) commit transaction Redirected to localhost:3000/classifieds/8 Completed 302 Found in 5ms (ActiveRecord: 1.4ms), "contact", "created_at", "price",) >VALUES (?, ?, ?, ?) [["access", "bons"], ["contact", "918218338"], ["created_at" , Sun, 19 >May 2013 22:39:43 UTC +00:00], ["price", 1500], ["updated_at", Sun, 2013 年 5 月 19 日 22:39:43 UTC >+00:00]] (0.8ms) commit transaction Redirected to localhost:3000/classifieds/8 Completed 302 Found in 5ms (ActiveRecord: 1.4ms)8ms) commit transaction Redirected to localhost:3000/classifieds/8 Completed 302 Found in 5ms (ActiveRecord: 1.4ms)8ms) commit transaction Redirected to localhost:3000/classifieds/8 Completed 302 Found in 5ms (ActiveRecord: 1.4ms)

如您所见,它插入到“分类”表中,但没有插入到“分类图像”表中,而且,我没有从回形针中获得任何信息......

对不起所有的代码,但这应该是我没有看到的简单的东西,并且随着您获得的更多信息,您可以更好地帮助我......如果您需要更多代码或信息,请告诉我.. .

4

1 回答 1

1

我们花了几天的时间来解决类似的问题。最后,是模型中调用的:reject_iflambda在错误情况下触发。accepts_nested_attributes_for

现在我重新审视这个问题,看来你有同样的问题。代替:

:reject_if => lambda { |t| t['classified_image'].blank? }

你可能应该有:

:reject_if => lambda { |t| t['photo'].blank? }

即回形针属性的名称而不是嵌套模型。


出错是一件令人沮丧的事情,因为它会默默地失败,t['classified_image']并且会nil一直出现,并且您的属性将按照指定的方式被拒绝。:) 至少我们学会了更加小心:reject_if......

于 2013-07-17T11:58:40.010 回答