I''ve been fighting with myself over this for the past week, and would like to hear some others'' thoughts on the subject. I essentially have the following models in place: class Item < ActiveRecord::Base has_many :images end class Image < ActiveRecord::Base belongs_to :item file_column :filename end In my view, I have javascript that basically creates N file_column_fields for uploading multiple images. The problem I''m facing is in the controller. What is the best method for getting all of these files through the has_many and into file_column? I''ve tried a number of variations on Image.new to no avail. I know someone out there on the interweb has been through this before, but the docs for file_column are terrible and the author doesn''t answer his email (despite my desperate jumping through his anti-spam hoops). TIA. -- Posted via http://www.ruby-forum.com/.
On Fri, 2006-07-14 at 23:48 +0200, Brandon Mitchell wrote:> I''ve been fighting with myself over this for the past week, and would > like to hear some others'' thoughts on the subject. I essentially have > the following models in place: > > class Item < ActiveRecord::Base > has_many :images > end > > class Image < ActiveRecord::Base > belongs_to :item > file_column :filename > end > > In my view, I have javascript that basically creates N > file_column_fields for uploading multiple images. The problem I''m facing > is in the controller. What is the best method for getting all of these > files through the has_many and into file_column? I''ve tried a number of > variations on Image.new to no avail. > > I know someone out there on the interweb has been through this before, > but the docs for file_column are terrible and the author doesn''t answer > his email (despite my desperate jumping through his anti-spam hoops). > TIA.---- this wouldn''t seem to be a problem with file_column or with has_many but rather a problem of programming and logic. You probably need to create an array or a hash in your view and return that to your code with then iterates through the returned values and saves them. for each params[:image][:file] do @image = Image.new @image.item_id = params[:item_id] @image.filename = params[:image][:file] Image.save = @image end you probably have to add a bunch of checking and a flash notice, etc. but that is the general structure of what I am thinking and it''s hot and I''m tired Craig
Brandon: I think you''re over-engineering this. All you really need to do is look at how other sites do this... a good example is gmail''s attachment system. Top half of the screen has the file form. It contains a field to upload the box. It also contains a hidden form field that contains the item_id <%=file_column_field "image", "filename" %> <input type="hidden" name="image[item_id]" value = <%=@item.id %> <%=submit_tag "Upload Image" %> The bottom half of the screen renders a partial that lists all files for an item. Submit the file... re-display this page again. Highlight the one you just added. None of this requires JavaScript and it''s actually very intuitive, plus it''s a lot easier to code. I may be one of the few people that do things this way, but I will almost ALWAYS just assign the foreign key as opposed to creating an unnecessary object instance just so I can use << to associate the child element. It''s easier and faster just to assign the foreign key directly. You already know what item you''re attaching the files to anyway. Would that solution work for you, or am I missing your intentions? On 7/14/06, Craig White <craigwhite@azapple.com> wrote:> > On Fri, 2006-07-14 at 23:48 +0200, Brandon Mitchell wrote: > > I''ve been fighting with myself over this for the past week, and would > > like to hear some others'' thoughts on the subject. I essentially have > > the following models in place: > > > > class Item < ActiveRecord::Base > > has_many :images > > end > > > > class Image < ActiveRecord::Base > > belongs_to :item > > file_column :filename > > end > > > > In my view, I have javascript that basically creates N > > file_column_fields for uploading multiple images. The problem I''m facing > > is in the controller. What is the best method for getting all of these > > files through the has_many and into file_column? I''ve tried a number of > > variations on Image.new to no avail. > > > > I know someone out there on the interweb has been through this before, > > but the docs for file_column are terrible and the author doesn''t answer > > his email (despite my desperate jumping through his anti-spam hoops). > > TIA. > ---- > this wouldn''t seem to be a problem with file_column or with has_many but > rather a problem of programming and logic. > > You probably need to create an array or a hash in your view and return > that to your code with then iterates through the returned values and > saves them. > > for each params[:image][:file] do > @image = Image.new > @image.item_id = params[:item_id] > @image.filename = params[:image][:file] > Image.save = @image > end > > you probably have to add a bunch of checking and a flash notice, etc. > but that is the general structure of what I am thinking and it''s hot and > I''m tired > > Craig > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060715/116b0729/attachment-0001.html
Brian Hogan wrote:> I think you''re over-engineering this.I agree. That''s why I posted here for a reality check. ;-) However, due to restrictions in the flow of the site itself, I need to upload and associate the images with the Item upon initial creation. I was hoping that I could make some magic happen in the models such that in Item#create I would simply be doing an iterative <<, but that preference is purely stylistic at this point. In that veign, here is how I had envisioned the flow of the creation of a new Item: Item#create receives params[] from view. Within the Item model''s #new, any images in params[:images] are added like: @params[:images].each do |i| @item.images << Image.new[i] end This would leave Item#create with one line to create a new item with all associated images: @item = Item.new[params[:item]] Where params[:item][:images] is populated dynamically based upon the quanitity of upload fields the user chooses. Is this too much logic in the model? Given the requirement for adding N images at Item#create, how would you do it? -- Posted via http://www.ruby-forum.com/.
I would do it just as I stated prevously. On a real estate site, I allow the user to create a property and add photos at the same time... using the method I outlined above. They add them one at a time, so I don''t have to ask them how many they plan to add. It works out well, it''s simple to implement, and the process is easy. As I said, I don''t like looping and associationg with << because it''s not necessary here. YOu have the foreign key... just do Image.create params[:image], making sure that your image array contains the item_id column. Lots of luck to you if you really can''t do it this way. You can get file_column to do multiple images on the same page but it''s more difficult. On 7/15/06, Brandon Mitchell <magicsmoke@gmail.com> wrote:> > Brian Hogan wrote: > > > I think you''re over-engineering this. > > I agree. That''s why I posted here for a reality check. ;-) > > However, due to restrictions in the flow of the site itself, I need to > upload and associate the images with the Item upon initial creation. I > was hoping that I could make some magic happen in the models such that > in Item#create I would simply be doing an iterative <<, but that > preference is purely stylistic at this point. In that veign, here is how > I had envisioned the flow of the creation of a new Item: > > Item#create receives params[] from view. > Within the Item model''s #new, any images in params[:images] are added > like: > @params[:images].each do |i| > @item.images << Image.new[i] > end > > This would leave Item#create with one line to create a new item with all > associated images: > @item = Item.new[params[:item]] > Where params[:item][:images] is populated dynamically based upon the > quanitity of upload fields the user chooses. > > Is this too much logic in the model? Given the requirement for adding N > images at Item#create, how would you do it? > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060715/d888d629/attachment.html
Brian Hogan wrote:> Lots of luck to you if you really can''t do it this way. You can get > file_column to do multiple images on the same page but it''s more > difficult.Agreed. That''s what I''ve been experiencing. I certainly appreciate the advice, and will give it a shot. Thanks. -- Posted via http://www.ruby-forum.com/.