【问题标题】:Paperclip and nested attributes - not writing to the database回形针和嵌套属性 - 不写入数据库
【发布时间】: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


    【解决方案1】:

    我们花了好几天的时间来解决一个类似的问题。最后是模型中 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更加小心...

    【讨论】:

    • 谢谢,它成功了,这是一个简单的细节 :) 现在我无法显示照片,但这是我正在解决的另一个问题,感谢您的回复!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    相关资源
    最近更新 更多