回形针和嵌套属性 - 不写入数据库

回形针和嵌套属性 - 不写入数据库

问题描述:

我想上传一个表单上多个文件。我遵循轨道铸造回形针和嵌套属性,也是本教程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 

这里的日志的一个例子,当我提交形式:

开始POST “/ classifieds”为127.0.0.1于2013-05-19 23:39:43 +0100 通过ClassifiedsController处理#以HTML格式创建 参数:{“utf8”=>“✓”,“authenticity_token”=>“978KGJSUlmMEvr6Tysg5xYIEQzNLn5vod07g + Z7njkU = “ ”分类“=> { ”接触“=> ”918218338“, ”价格“=> ”1500“, ”访问“=> ”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 ;名= \ “分类[classified_images_attributes] [0] [照片] \”; filename = \“064_dont-count-the-days.jpg \”\ r \ nContent-Type:image/jpeg \ r \ n“,> @ tempfile =#3954-11t04t >>},”1“=> {” (0.1ms)开始交易 SQL(0.5ms)=>“>”>},“2”=> {“caption”=>“” INSERT INTO“classifieds”(“access”,“contact”,“created_at”,“price”,)> VALUES(?,?,?,?)[[“access”,“bons”],[“contact” 2013年5月19日星期日22:39:15 UTC +00:00] 43 UTC> 00:00]] (为0.8ms)提交重定向到本地主机事务 :3000 /分类/ 8 完成302在5ms的实测值(ActiveRecord的:1.4ms之)

正如你可以看到,它插入“classifi编辑“表,但不进入”classifieds_image“表,并且,我没有从回形针获得任何信息...

对不起所有的代码,但这应该是真的很简单,我没有看到,作为更多的信息,你有更好的帮助我...请让我知道,如果你需要更多的代码或信息...

我们花了几天时间追逐类似的问题。最后,在错误情况下触发模型中的accepts_nested_attributes_for调用的:reject_if lambda。

现在我再次探讨这个问题,似乎你有同样的问题。相反的:

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

你应该有:

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

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


这是一个令人沮丧的事情得到错误的,因为它静静地失败,t['classified_image']nil所有的时间和指定的属性将被拒绝。 :)至少我们学会了更加小心:reject_if ...

+0

谢谢,它的工作,这是一个简单的细节:)现在我有麻烦显示照片,但这是我解决的另一个问题,谢谢期待你的答复! – lbramos