I'm working on a paperclip upload system where I have a User who has many Uploads which in turn have many Upload_Images relationship.
Due to the nature of how my system is meant to work, a User is created once and then has uploads added in the future and Upload records need to be created in the User controllers update action. Originally it was just the Upload record that needed to be created, but I had to extend the functionality to include an Upload_image. As it stands, the Upload record is created, but not the upload_image.
He's the params hash for where the Update method is getting called after the New upload form (with upload image) is sent.
"_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"}
As you can see the parameters for both the Upload file and the Upload_Image file are sent, but only the Upload is persisted to the database.
My Update method:
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
Here's my Models (I removed extra stuff for readability):
User
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
Upload
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
If I take out the
:reject_if => lambda { |t| t[':upload_images'].nil? }
in my uploads model I get an unkown attribute: upload_image error in my users controller when I attempt the update.
There were 2 problems that prevented the persistence of my Upload_Image.