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

时间:2013-05-19 23:59:55

标签: sqlite ruby-on-rails-3.2 paperclip nested-attributes

我正在尝试在表单上上传多个文件。我跟着铁轨演员的回形针和嵌套属性,以及本教程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开始为127.0.0.1发布“/ classifieds”   由ClassifiedsController处理#create as HTML    参数:{“utf8”=&gt;“✓”,“authenticity_token”=&gt;“978KGJSUlmMEvr6Tysg5xYIEQzNLn5vod07g + Z7njkU =”,“classified”=&gt; {“contact”=&gt;“918218338”,“price”=&gt;“ 1500“,”access“=&gt;”bons“,”classified_images_attributes“=&gt; {”0“=&gt; {”caption“=&gt;”teste“,”photo“=&gt; #@ original_filename =”064_dont- count-the-days.jpg“,@ content_type =”image / jpeg“,&gt; @ headers =”Content-Disposition:form-data; name = \“classified [classified_images_attributes] [0] [photo] \”; filename = \“064_dont-count-the-days.jpg \”\ r \ nConContent-Type:image / jpeg \ r \ n“,&gt; @ tempfile =#3954-11t04t&gt;&gt;},”1“=&gt; {“caption”=&gt;“”},“2”=&gt; {“caption”=&gt;“”}}},“commit”=&gt;“Criar novo&gt; Classificado”}     (0.1ms)开始交易    SQL(0.5ms)INSERT INTO“classifieds”(“access”,“contact”,“created_at”,“price”,)&gt; VALUES(?,?,?,?)[[“access”,“bons”] ,[“contact”,“918218338”],[“created_at”,Sun,19&gt; 2013年5月22:39:43 UTC +00:00],[“price”,1500],[“updated_at”,Sun, 2013年5月19日22:39:43 UTC&gt; +00:00]]     (0.8ms)提交事务   重定向到localhost:3000 / classifieds / 8   完成302发现在5ms(ActiveRecord:1.4ms)

正如您所看到的,它会插入“classifieds”表中,但不会插入“classifieds_image”表中,而且,我也没有从回形针中获取任何信息......

对不起所有的代码,但这应该是我看不到的简单的东西,因为你有更多的信息,你可以帮助我更好...如果你需要更多的代码或者请告诉我信息...

1 个答案:

答案 0 :(得分:1)

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

现在我再次回答这个问题,似乎你有同样的问题。而不是:

:reject_if => lambda { |t| t['classified_image'].blank? }
你可能应该:

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

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


出错是件令人沮丧的事情,因为它无声地失败,t['classified_image']将一直nil,你的属性将被指定拒绝。 :)至少我们学会了对:reject_if ...

更加小心