Hi all, I''m trying to do a file upload. I have followed the examples in Agile Web Development with Rails, and Rails Cookbook, and I can''t get it to work!! The following code is from the Rails Cookbook. I have two tables: Items & Photos. It''s supposed to save Name and Description into the Items table, and then grab the photo information and put it in the Photo table. The Item information is saving, but I''m blowing up in the Photo Model. It doesn''t recognize the field names for the Photo table and also doesn''t recognize the "read" attribute for the image. Any idea what I''m missing?? As always, any and all help is greatly appreciated!!! Thanks! ~Ali ITEMS CONTROLLER def new end def create @item = Item.new(params[:item]) # I don''t understand this part -- how can it ever get past here??? I tried commenting it out, but still won''t work. if @item.save flash[:error] = ''Problem Uploading'' redirect_to :action => ''new'' return end @photo = Photo.new(params[:photo]) @photo.item_id = @item.id if @photo.save flash[:notice] = ''Item was successfully created.'' redirect_to :action => ''list'' else flash[:error] = ''There was a problem.'' render :action => ''new'' end end ITEMS MODEL class Item < ActiveRecord::Base has_many :photos end PHOTOS MODEL class Photo < ActiveRecord::Base belongs_to :item, :foreign_key => "item_id" def photo=(image_field) self.name = base_part_of(image_field.original_filename) self.content_type = image_field.content_type.chomp self.data = image_field.read end def base_part_of(file_name) name = File.basename(file_name) name.gsub(/[^\w._-]/, '''') end end ITEM/_FORM <div class="errorExplanation p"><%= flash[:error] %></div> <!--[form:item]--> <p><label for="item_name">Name</label><br/> <%= text_field ''item'', ''name'' %></p> <p><label for="item_description">Description</label><br/> <%= text_field ''item'', ''description'' %></p> <p><label for="photo">Photo</label><br/> <%= file_field(''photo'', ''photo'') %></p> <!--[eoform:item]--> ITEM/NEW <h1>New Item</h1> <%= start_form_tag :action => ''create'', :id => @item, :multipart => true %> <%= render :partial => ''form'' %> <%= submit_tag "Create" %> <%= end_form_tag %> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi again, No one has responded to my post and I''m wondering if I worded it in a confusing way? I really need some help with this. I have been scouring books and the web to no avail. If further clarification is needed, please ask! I would really appreciate any suggestions!! Thanks again!! ~Ali On Mar 13, 9:52 am, "Ali" <awilli...-KQhSGARDxiOeQVqA2Ba1Pg@public.gmane.org> wrote:> Hi all, > > I''m trying to do a fileupload. I have followed the examples in Agile > Web Development with Rails, and Rails Cookbook, and I can''t get it to > work!! The following code is from the Rails Cookbook. I have two > tables: Items & Photos. It''s supposed to save Name and Description > into the Items table, and then grab the photo information and put it > in the Photo table. The Item information is saving, but I''m blowing > up in the Photo Model. It doesn''t recognize the field names for the > Photo table and also doesn''t recognize the "read" attribute for theimage. Any idea what I''m missing?? As always, any and all help is > greatly appreciated!!! Thanks! ~Ali > > ITEMS CONTROLLER > > def new > end > > def create > @item = Item.new(params[:item]) > > # I don''t understand this part -- how can it ever get past here??? > I tried commenting it out, but still won''t work. > if @item.save > flash[:error] = ''Problem Uploading'' > redirect_to :action => ''new'' > return > end > > @photo = Photo.new(params[:photo]) > @photo.item_id = @item.id > > if @photo.save > flash[:notice] = ''Item was successfully created.'' > redirect_to :action => ''list'' > else > flash[:error] = ''There was a problem.'' > render :action => ''new'' > end > end > > ITEMS MODEL > > class Item < ActiveRecord::Base > has_many :photos > end > > PHOTOS MODEL > > class Photo < ActiveRecord::Base > > belongs_to :item, :foreign_key => "item_id" > > def photo=(image_field) > self.name = base_part_of(image_field.original_filename) > self.content_type = image_field.content_type.chomp > self.data = image_field.read > end > > def base_part_of(file_name) > name = File.basename(file_name) > name.gsub(/[^\w._-]/, '''') > end > > end > > ITEM/_FORM > > <div class="errorExplanation p"><%= flash[:error] %></div> > > <!--[form:item]--> > > <p><label for="item_name">Name</label><br/> > <%= text_field ''item'', ''name'' %></p> > > <p><label for="item_description">Description</label><br/> > <%= text_field ''item'', ''description'' %></p> > > <p><label for="photo">Photo</label><br/> > <%= file_field(''photo'', ''photo'') %></p> > > <!--[eoform:item]--> > > ITEM/NEW > > <h1>New Item</h1> > > <%= start_form_tag :action => ''create'', :id => @item, :multipart => > true %> > <%= render :partial => ''form'' %> > <%= submit_tag "Create" %> > <%= end_form_tag %>--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ali wrote:> Hi again, > No one has responded to my post and I''m wondering if I worded it in a > confusing way? I really need some help with this. I have been > scouring books and the web to no avail. If further clarification is > needed, please ask! I would really appreciate any suggestions!! > > Thanks again!! > ~AliWhat does your HTML form tag look like? I think your form tag is funky since :multipart => true is being considered part of the url. If so your form tag would look like: <form action="/foo/create/123?multipart=true"> But you need it to be <form action="/foo/create/123" enctype="multipart/form-data"> You do that with form tag like this, so that the :multipart key is not part of the url hash: <%= form_tag({:action => ''create'', :id => @item}, {:multipart => true}) %> This way the multipart is passed in as a second hash rather than tacked onto the first. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
andrew.ohnstad-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-15 18:35 UTC
Re: Uploading Images
On Mar 13, 11:52 am, "Ali" <awilli...-KQhSGARDxiOeQVqA2Ba1Pg@public.gmane.org> wrote:> def create > @item = Item.new(params[:item]) > > # I don''t understand this part -- how can it ever get past here??? > I tried commenting it out, but still won''t work. > if @item.save > flash[:error] = ''Problem Uploading'' > redirect_to :action => ''new'' > return > end >Yeah, this is a problem. Look at what you have here... "If the item is successfully saved then flash error redirect to new stop processing" While this won''t prevent your item from being saved, it is causing the controller to stop processing before the photo statements are called. Try ''unless @item.save'' I''m also not sure that the photo uploading part of your photo model will ever get called, since this form is in the /user application... Uploads are something I struggled with myself, so I''m not going to say much more about that. In my application the form only handles creating an upload, not an item _and_ and upload. (There is a seperate form for creating the item.) Good luck! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
It''s working!! Thank you so much!! I love this forum - I''d be lost without it!! On Mar 13, 9:52 am, "Ali" <awilli...-KQhSGARDxiOeQVqA2Ba1Pg@public.gmane.org> wrote:> Hi all, > > I''m trying to do a fileupload. I have followed the examples in Agile > Web Development with Rails, and Rails Cookbook, and I can''t get it to > work!! The following code is from the Rails Cookbook. I have two > tables: Items & Photos. It''s supposed to save Name and Description > into the Items table, and then grab the photo information and put it > in the Photo table. The Item information is saving, but I''m blowing > up in the Photo Model. It doesn''t recognize the field names for the > Photo table and also doesn''t recognize the "read" attribute for theimage. Any idea what I''m missing?? As always, any and all help is > greatly appreciated!!! Thanks! ~Ali > > ITEMS CONTROLLER > > def new > end > > def create > @item = Item.new(params[:item]) > > # I don''t understand this part -- how can it ever get past here??? > I tried commenting it out, but still won''t work. > if @item.save > flash[:error] = ''Problem Uploading'' > redirect_to :action => ''new'' > return > end > > @photo = Photo.new(params[:photo]) > @photo.item_id = @item.id > > if @photo.save > flash[:notice] = ''Item was successfully created.'' > redirect_to :action => ''list'' > else > flash[:error] = ''There was a problem.'' > render :action => ''new'' > end > end > > ITEMS MODEL > > class Item < ActiveRecord::Base > has_many :photos > end > > PHOTOS MODEL > > class Photo < ActiveRecord::Base > > belongs_to :item, :foreign_key => "item_id" > > def photo=(image_field) > self.name = base_part_of(image_field.original_filename) > self.content_type = image_field.content_type.chomp > self.data = image_field.read > end > > def base_part_of(file_name) > name = File.basename(file_name) > name.gsub(/[^\w._-]/, '''') > end > > end > > ITEM/_FORM > > <div class="errorExplanation p"><%= flash[:error] %></div> > > <!--[form:item]--> > > <p><label for="item_name">Name</label><br/> > <%= text_field ''item'', ''name'' %></p> > > <p><label for="item_description">Description</label><br/> > <%= text_field ''item'', ''description'' %></p> > > <p><label for="photo">Photo</label><br/> > <%= file_field(''photo'', ''photo'') %></p> > > <!--[eoform:item]--> > > ITEM/NEW > > <h1>New Item</h1> > > <%= start_form_tag :action => ''create'', :id => @item, :multipart => > true %> > <%= render :partial => ''form'' %> > <%= submit_tag "Create" %> > <%= end_form_tag %>--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---