I am new to Ruby/Rails, and I'm writing an app that allows the user to upload mulitple images to attach to a user. The user data is stored in the Entity model, and the picture data (including file_column) is stored in the Picture model, which has a foreign key to the entity.id. I want to store the pictures in a directory that has the Entity's id as the last directory name, not the picture's id. Base upon what I've read, I have to use a callback procedure in Picture to create the appropriate directory. This is what I came up with:
_*entity.rb*_ class Entity < ActiveRecord::Base validates_presence_of :lastname, :address1, :zip, :phone
belongs_to :entitytype has_and_belongs_to_many :entitysubtypes belongs_to :state belongs_to :city belongs_to :agency belongs_to :agent belongs_to :screentemplate has_many :comments has_many :pictures end
*_picture.rb_* class Picture < ActiveRecord::Base belongs_to :entity file_column :image, :web_root => "/picture/entity/", :store_dir => :new_store_dir
def new_stor_dir return "/picture/entity/" + :entity.id + "/" end
_*entity_controller.rb*_ def upload @entity = Entity.find(params["id"]) @pictures = @entity.pictures.build(params[:pictures]) @entity.pictures.each { |pic| pic.attributes = params[:picture][pic.id.to_s] } if @entity.update_attributes(params[:entity]) flash[:notice] = "Pictures uploaded." else flash[:notice] = "Problem uploading pictures." end render :action => 'show', :id => @entity end
I must be doing something wrong because I get no file name in the database. I suspect that, since it worked fine without the callback label in store_dir, it must be something I'm doing. Any ideas? Any help would be greatly appreciated!
Thanks.