gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 19:50:33 25 4
gpt4 key购买 nike

我正在尝试在表单上上传多个文件。我遵循了回形针和嵌套属性的导轨铸件,以及本教程 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

在分类 Controller 上,在"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

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

Started POST "/classifieds" for 127.0.0.1 at 2013-05-19 23:39:43 +0100 Processing by ClassifiedsController#create as HTML Parameters: {"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) begin transaction 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) commit transaction Redirected to localhost:3000/classifieds/8 Completed 302 Found in 5ms (ActiveRecord: 1.4ms)



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

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

最佳答案

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

关于sqlite - 回形针和嵌套属性 - 不写入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16640779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com