我正在开发一个回形针上传系统,其中我有一个用户,他有许多上传,而这些上传又有许多Upload_Images关系。
由于我的系统工作方式的性质,用户创建一次,然后在将来添加上传,并且需要在用户控制器更新操作中创建上传记录。 最初只是需要创建的上传记录,但我必须扩展功能以包含Upload_image。目前,将创建上传记录,但不创建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"rnContent-Type: application/octet-streamrn",
@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"rnContent-Type: image/pngrn",
@tempfile=#<File:/tmp/RackMultipart20120505-2595-16gf0jl>>,
"preview"=>"0"}}}},
"username"=>"chunter"},
"commit"=>"Submit Upload",
"id"=>"chunter"}
如您所见,上传文件和Upload_Image文件的参数都已发送,但仅上传将保存到数据库中。
我的更新方法:
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
Upload_Images
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? }
在我的上传模型中,我得到一个 unkown 属性:当我尝试更新时,我的用户控制器upload_image错误。
有 2 个问题阻止了我的Upload_Image的持久性。
- 我的upload_image模型中的以下行:reject_if => lambda { |t| t[':upload_images'].nil? } 导致我的持久性静默失败,因为我尝试保留的文件上传中存在 nil 值。
- 我得到了未知属性:upload_image因为在我看来我有一个file_field :upload_image但在我的模型中,我将图像附件称为:image。这导致reject_if lamba 在我的模型中触发