【发布时间】:2013-05-19 23:59:55
【问题描述】:
我正在尝试在一个表单上上传多个文件。我遵循了回形针和嵌套属性的轨道铸件,以及本教程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
在分类控制器上,在“新”部分,我有: 定义新 @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 为 127.0.0.1 开始 POST "/classifieds" 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: image/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",) >VALUES (?, ?, ?, ?) [["access", "bons"], ["contact", "918218338"], ["created_at", Sun, 19 > May 22:39:43 UTC +00:00], ["price", 1500], ["updated_at", Sun, 5 月 19 日2013 年 22:39:43 UTC >+00:00]] (0.8ms) 提交事务 重定向到 localhost:3000/classifieds/8 完成 302 Found in 5ms (ActiveRecord: 1.4ms)
如您所见,它插入到“分类”表中,但没有插入到“分类图像”表中,而且,我没有从回形针获得任何信息......
对不起所有代码,但这应该是我没有看到的简单的东西,并且随着您获得的信息越多,您可以更好地帮助我...如果您需要更多代码或信息...
【问题讨论】:
标签: sqlite ruby-on-rails-3.2 paperclip nested-attributes