oliver barnes
2005-Aug-09 22:25 UTC
[newbie] upload problem when using model + controller inheritance
hi, using STI i have a model Medium mapping a Media table on the db. I extend Medium in creating Photo, Video and Audio models. I had different controllers for each of the specialized models, saving to the db and the file to the filesystem just fine. then I noticed these controllers were very similar to each other as wel, so I created a MediumController, from which I extend a PhotoController, etc. the db save works fine, but the uploaded file param doesn''t get mapped to the respective ''file'' attribute in the Photo model, for instance, though it''s available in the request as photo=>{"file"=>#<StringIO:...>}. can''t figure out why this is... here''s the scoop so it makes sense: class Medium < ActiveRecord::Base belongs_to :user attr_accessor :file attr_accessor :format def after_create #path to be stored in the db and to be used for img srcs path = "..." #content type gotten from the file itself content_type = file.content_type.strip File.open(path, "w") { |f| f.write(file.read) } update_attribute(:path) end end class MediumController < ApplicationController def upload @medium = mymodel.new(params[:medium]) @medium.user = User.find(params[:user][:id]) if @medium.save flash[:notice] = "#{@medium.type} was uploaded successfully" else flash[:notice] = "There was a problem uploading the # {@medium.type.downcase}, please try again" end end end class PhotoController < MediumController #got this solution from http://www.oreillynet.com/pub/wlg/7403 def mymodel Photo end end any comments will be greatly appreciated! I love to be able to use inheritance like this, but I''ve spent so much time on figuring this out, I''m almost commiting DRY heresy and going back to using models with repetitive code ;) -Oliver
Gianni Jacklone
2005-Aug-10 18:21 UTC
Re: [newbie] upload problem when using model + controller inheritance
Hi Oliver, Your approach to using inheritance in controllers and models to reuse code will have no impact on why the file param is not getting mapped tot he file attribute in one of your models. You just have a bug some where. By the code you''ve shown below it seams you have a photo param that holds the file, but in your MediumController you''re passing params[:medium]. Maybe you need to change the file upload field to be called medium instead of photo, or some other similar bug. Just take a closer look, it''s probably just a little mistake -Gianni oliver barnes wrote:> hi, > using STI i have a model Medium mapping a Media table on the db. I > extend Medium in creating Photo, Video and Audio models. > > I had different controllers for each of the specialized models, > saving to the db and the file to the filesystem just fine. > > then I noticed these controllers were very similar to each other as > wel, so I created a MediumController, from which I extend a > PhotoController, etc. the db save works fine, but the uploaded file > param doesn''t get mapped to the respective ''file'' attribute in the > Photo model, for instance, though it''s available in the request as > photo=>{"file"=>#<StringIO:...>}. can''t figure out why this is... > > here''s the scoop so it makes sense: > > class Medium < ActiveRecord::Base > belongs_to :user > > attr_accessor :file > attr_accessor :format > > def after_create > #path to be stored in the db and to be used for img srcs > path = "..." > > #content type gotten from the file itself > content_type = file.content_type.strip > > File.open(path, "w") { |f| f.write(file.read) } > > update_attribute(:path) > end > end > > class MediumController < ApplicationController > def upload > @medium = mymodel.new(params[:medium]) > @medium.user = User.find(params[:user][:id]) > > if @medium.save > flash[:notice] = "#{@medium.type} was uploaded successfully" > else > flash[:notice] = "There was a problem uploading the # > {@medium.type.downcase}, please try again" > end > > end > end > > class PhotoController < MediumController > #got this solution from http://www.oreillynet.com/pub/wlg/7403 > def mymodel > Photo > end > end > > any comments will be greatly appreciated! I love to be able to use > inheritance like this, but I''ve spent so much time on figuring this > out, I''m almost commiting DRY heresy and going back to using models > with repetitive code ;) > -Oliver > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
oliver barnes
2005-Aug-10 18:50 UTC
Re: [newbie] upload problem when using model + controller inheritance
Hi Gianni, thanks for your response. I agree, it must be a little mistake... but from what I understood, I should be able to use the subclass in the view (uploading the file to photo=>file) and Medium should recognize it as being of its own type, right? photo gets handled by photo_controller, which passes it on to Photo... which inherits the file attribute? am I missing something? I''m thinking I have to specify the mapping somewhere, like I did for photo_controller, so that medium_controller would know what type of model it''s dealing with. does this make sense? best Oliver On Aug 10, 2005, at 3:21 PM, Gianni Jacklone wrote:> Hi Oliver, > > Your approach to using inheritance in controllers and models to > reuse code will have no impact on why the file param is not getting > mapped tot he file attribute in one of your models. > > You just have a bug some where. By the code you''ve shown below it > seams you have a photo param that holds the file, but in your > MediumController you''re passing params[:medium]. Maybe you need to > change the file upload field to be called medium instead of photo, > or some other similar bug. > > Just take a closer look, it''s probably just a little mistake > > -Gianni > > oliver barnes wrote: > > >> hi, >> using STI i have a model Medium mapping a Media table on the db. >> I extend Medium in creating Photo, Video and Audio models. >> >> I had different controllers for each of the specialized models, >> saving to the db and the file to the filesystem just fine. >> >> then I noticed these controllers were very similar to each other >> as wel, so I created a MediumController, from which I extend a >> PhotoController, etc. the db save works fine, but the uploaded >> file param doesn''t get mapped to the respective ''file'' attribute >> in the Photo model, for instance, though it''s available in the >> request as photo=>{"file"=>#<StringIO:...>}. can''t figure out why >> this is... >> >> here''s the scoop so it makes sense: >> >> class Medium < ActiveRecord::Base >> belongs_to :user >> >> attr_accessor :file >> attr_accessor :format >> >> def after_create >> #path to be stored in the db and to be used for img srcs >> path = "..." >> >> #content type gotten from the file itself >> content_type = file.content_type.strip >> >> File.open(path, "w") { |f| f.write(file.read) } >> >> update_attribute(:path) >> end >> end >> >> class MediumController < ApplicationController >> def upload >> @medium = mymodel.new(params[:medium]) >> @medium.user = User.find(params[:user][:id]) >> >> if @medium.save >> flash[:notice] = "#{@medium.type} was uploaded >> successfully" >> else >> flash[:notice] = "There was a problem uploading the # >> {@medium.type.downcase}, please try again" >> end >> >> end >> end >> >> class PhotoController < MediumController >> #got this solution from http://www.oreillynet.com/pub/wlg/7403 >> def mymodel >> Photo >> end >> end >> >> any comments will be greatly appreciated! I love to be able to >> use inheritance like this, but I''ve spent so much time on >> figuring this out, I''m almost commiting DRY heresy and going back >> to using models with repetitive code ;) >> -Oliver >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >