0

我正在开发一个回形针上传系统,我有一个用户,他有很多 Uploads,而 Uploads 又有很多 Upload_Images 关系。

由于我的系统工作方式的性质,用户创建一次,然后在将来添加上传,并且需要在用户控制器更新操作中创建上传记录。最初只需要创建上传记录,但我必须扩展功能以包含 Upload_image。就目前而言,创建的是 Upload 记录,而不是 upload_image。

他是发送新上传表单(带有上传图片)后调用 Update 方法的参数哈希。

"_method"=>"put",
 "authenticity_token"=>"fEDOZfJ6UarMD/nNM7t86zYXrEkTFtXyrXKJglYZ0Jw=",
 "user"=>{"uploads_attributes"=>{"0"=>{"id"=>"37"},
 "1"=>{"id"=>"36"},
 "2"=>{"id"=>"35"},
 "3"=>{"id"=>"34"},
 "4"=>{"id"=>"33"},
 "5"=>{"id"=>"32"},
 "6"=>{"id"=>"31"},
 "7"=>{"id"=>"30"},
 "8"=>{"id"=>"29"},
 "9"=>{"id"=>"28"},
 "10"=>{"id"=>"27"},
 "11"=>{"id"=>"26"},
 "12"=>{"id"=>"25"},
 "13"=>{"id"=>"24"},
 "14"=>{"id"=>"23"},
 "15"=>{"id"=>"22"},
 "16"=>{"id"=>"21"},
 "17"=>{"id"=>"20"},
 "18"=>{"id"=>"19"},
 "19"=>{"id"=>"18"},
 "20"=>{"id"=>"17"},
 "21"=>{"id"=>"16"},
 "22"=>{"id"=>"15"},
 "23"=>{"id"=>"14"},
 "24"=>{"id"=>"13"},
 "25"=>{"id"=>"12"},
 "26"=>{"id"=>"11"},
 "27"=>{"id"=>"10"},
 "28"=>{"upload"=>#<ActionDispatch::Http::UploadedFile:0x00000004103d18 @original_filename="robot.rres",
 @content_type="application/octet-stream",
 @headers="Content-Disposition: form-data; name=\"user[uploads_attributes][28][upload]\"; filename=\"robot.rres\"\r\nContent-Type: application/octet-stream\r\n",
 @tempfile=#<File:/tmp/RackMultipart20120505-2595-lmr2j4>>,
 "name"=>"Test",
 "file_description"=>"Test",
 "private"=>"0",
 "file_category"=>"1",
 "upload_images_attributes"=>{"0"=>{"upload_image"=>#<ActionDispatch::Http::UploadedFile:0x00000004103a98 @original_filename="Low_Res_NAO_NextGen_04.png",
 @content_type="image/png",
 @headers="Content-Disposition: form-data; name=\"user[uploads_attributes][28][upload_images_attributes][0][upload_image]\"; filename=\"Low_Res_NAO_NextGen_04.png\"\r\nContent-Type: image/png\r\n",
 @tempfile=#<File:/tmp/RackMultipart20120505-2595-16gf0jl>>,
 "preview"=>"0"}}}},
 "username"=>"chunter"},
 "commit"=>"Submit Upload",
 "id"=>"chunter"}

如您所见,Upload 文件和 Upload_Image 文件的参数都已发送,但只有 Upload 被持久化到数据库中。

我的更新方法:

  def update
    @user = User.find_by_username(params[:id])
    #Update the users uploadS
    unless @user.update_attributes(params[:user])
      session[:error] = @user.errors.full_messages

      #render :action => "show"
      respond_to do |format|
        format.html { redirect_to :back, notice: 'Update unsuccessful.' }
        format.json { head :ok }
      end
    else
      respond_to do |format|
        format.html { redirect_to :back, notice: 'Update successful.' }
        format.json { head :ok }
      end
    end    
  end

这是我的模型(为了便于阅读,我删除了额外的东西):

用户

class User < ActiveRecord::Base

  attr_accessible :email, :password, :password_confirmation, :remember_me, :admin, :username, :avatar, :description, :name, :country, :province, :gender, :occupation, :city, :address, :birth_date, :uploads_attributes, :avatar_attachment_size, :avatar_file_type, :banned_until, :upload_images_attributes
  has_many :posts, :dependent => :destroy, :order => 'created_at desc'
  has_many :topics, :dependent => :destroy, :order => 'created_at desc'
  has_many :viewings, :dependent => :destroy, :order => 'updated_at desc'
  has_many :uploads, :dependent => :destroy, :order => 'created_at desc'
  has_many :news, :dependent => :destroy, :order => 'created_at desc'
  has_many :events, :dependent => :destroy, :order => 'date desc'
  has_many :blog_entries, :foreign_key => :user_id
  has_many :registers
  has_many :members
  has_many :teams, :through => :members

  accepts_nested_attributes_for :uploads, :allow_destroy => true
  has_attached_file :avatar, :styles => {:thumb=> "70x70#", :small  => "150x150>" }, :default_url => "/images/missing.jpg"


end

上传

  class Upload < ActiveRecord::Base
    include ActiveModel::Validations

    belongs_to :user
  belongs_to :upload_category, :foreign_key => :file_category  
  has_many :upload_images, :dependent => :destroy  
  accepts_nested_attributes_for :upload_images, :reject_if => lambda { |t| t[':upload_images'].nil? }
    has_attached_file :upload,  :path => ":rails_root/:class/:id/:basename.:extension", :url => ":rails_root/:class  /:id/:basename.:extension"

    attr_accessible :file_category, :file_description, :user_id, :upload, :name, :private, :uploads, :upload_images_attributes

    validates_attachment_size :upload, :less_than => 30.megabyte
    validates_presence_of :upload_file_name, :message => "must contain a valid file."
    validates :name, :presence => true
  validates :name, :length => { :maximum => 30 }

    validates_with FileValidator
end

上传_图片

class UploadImage < ActiveRecord::Base

belongs_to :upload
has_attached_file :image, :styles => { :small => "150x150>", :large => "320x240>"}, :default_url => "/images/missing.jpg"  


end

编辑

如果我拿出

 :reject_if => lambda { |t| t[':upload_images'].nil? }

在我的上传模型中,当我尝试更新时,我的用户控制器中出现了一个未知属性:upload_image 错误。

4

1 回答 1

0

有 2 个问题阻止了我的 Upload_Image 的持久性。

  1. 我的 upload_image 模型中的以下行:reject_if => lambda { |t| t[':upload_images'].nil? 导致我的持久性静默失败,因为我试图持久化的文件上传中存在 nil 值。
  2. 我得到了未知属性:upload_image,因为在我看来,我有一个 file_field :upload_image 但在我的模型中,我将图像附件称为 :image。这导致在我的模型中触发了reject_if lamba
于 2012-05-07T16:51:39.593 回答