Decio Silva
2005-Dec-31 03:34 UTC
[Rails] Validation errors not displaying and will not save
Hello all, I''m fairly new to rails so I''m probably doing something really stupid here.... I have been banging my head at this for hrs now... I''ve tried so many different things and nothing seems to get me any further... I am trying to create an image uploading page that will simultaneously create thumbnails to separate file directories and update the database via the ActiveRecord object..... but something seems to be wrong... when I test the validation does not seem to work (I deliberatly leave the "title" text_field black)... I never get my error_messages_for.. furthermore the object never saves even when I do fill out all the necessary fields....the if @web_image.save always returns false ..... I''m going mental..... model code: ------------------ class WebImage < ActiveRecord::Base #setup relationships # commented these out because thought they might be the prob #has_and_belongs_to_many :categories #has_and_belongs_to_many :articles #validation routines validates_presence_of :title protected def validate #doesn''t seem to work either, tried to force an erro up... errors.add(:title, "test validator") end end -------------- controller code blocks: -------------------- def image_upload @web_image = WebImage.new end def image_save @web_image = WebImage.new(params[:web_image]) if @web_image.save flash[:notice] = "Saved" else flash[:notice] = "Did not save " end redirect_to :action => ''image_upload'' end --------------------- view code of image_upload.rhtml ------------------------------- <% @page_title = "Upload New Image"%> <%= error_messages_for(''web_image'') %> <%= form_tag({:action => "image_save"}, :method => "post",:multipart => true )%> <table> <tr> <td>Image Title<br/> <%= text_field(:web_image, :title) %></td> <td width="30"> <!--spacer--></td> <td rowspan="3"> Image File <%= file_field(:uploader, :uploaded_file) %><br/><br/> Upload another? <%= check_box(:uploader, :another?, {}, :true, :false)%> </td> </tr> <tr> <td>Subtitle<br/> <%= text_field(:web_image, :subtitle) %></td> <tr> <td>Keywords<br/> <%= text_field(:web_image, :keywords) %></td> </tr> <tr> <td colspan="2"> <td align="center"><%= submit_tag("UPLOAD")%></td> </tr> </table> <%= end_form_tag -%> -- Posted via http://www.ruby-forum.com/.
Decio Silva
2005-Dec-31 17:20 UTC
[Rails] Re: Validation errors not displaying and will not save
Just in case anyone comes across this original posting, figured it out on my own eventually... the problem is the "redirect_to".... this should have been a render(:action=>''image_upload'') when you redirect as it turns out.... u lose all the instance values including the error object... cheers...> > controller code blocks: > -------------------- > def image_upload > @web_image = WebImage.new > end > > def image_save > @web_image = WebImage.new(params[:web_image]) > if @web_image.save > flash[:notice] = "Saved" > else > flash[:notice] = "Did not save " > end > redirect_to :action => ''image_upload'' > end > ----------------------- Posted via http://www.ruby-forum.com/.
Matt Goss
2006-Jan-03 17:15 UTC
[Rails] Validation errors not displaying and will not save
Hello Decio, It appears as though you are redirecting at the end of the "image_save" action. This would be your problem. A redirect causes the browser to reissue a call for the page to which you are being redirected. This will in effect call the image_upload method and then render the corresponding rhtml page, thus losing any state information you had on the call to image_save. To fix this, simply replace "redirect_to :action => ''image_upload''" render :action => "image_upload". Which will call the image_upload action after handling the image_save, BUT without leaving the context of a single http request. Matt -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Decio Silva Sent: Friday, December 30, 2005 10:35 PM To: rails@lists.rubyonrails.org Subject: [Rails] Validation errors not displaying and will not save Hello all, I''m fairly new to rails so I''m probably doing something really stupid here.... I have been banging my head at this for hrs now... I''ve tried so many different things and nothing seems to get me any further... I am trying to create an image uploading page that will simultaneously create thumbnails to separate file directories and update the database via the ActiveRecord object..... but something seems to be wrong... when I test the validation does not seem to work (I deliberatly leave the "title" text_field black)... I never get my error_messages_for.. furthermore the object never saves even when I do fill out all the necessary fields....the if @web_image.save always returns false ..... I''m going mental..... model code: ------------------ class WebImage < ActiveRecord::Base #setup relationships # commented these out because thought they might be the prob #has_and_belongs_to_many :categories #has_and_belongs_to_many :articles #validation routines validates_presence_of :title protected def validate #doesn''t seem to work either, tried to force an erro up... errors.add(:title, "test validator") end end -------------- controller code blocks: -------------------- def image_upload @web_image = WebImage.new end def image_save @web_image = WebImage.new(params[:web_image]) if @web_image.save flash[:notice] = "Saved" else flash[:notice] = "Did not save " end redirect_to :action => ''image_upload'' end --------------------- view code of image_upload.rhtml ------------------------------- <% @page_title = "Upload New Image"%> <%= error_messages_for(''web_image'') %> <%= form_tag({:action => "image_save"}, :method => "post",:multipart => true )%> <table> <tr> <td>Image Title<br/> <%= text_field(:web_image, :title) %></td> <td width="30"> <!--spacer--></td> <td rowspan="3"> Image File <%= file_field(:uploader, :uploaded_file) %><br/><br/> Upload another? <%= check_box(:uploader, :another?, {}, :true, :false)%> </td> </tr> <tr> <td>Subtitle<br/> <%= text_field(:web_image, :subtitle) %></td> <tr> <td>Keywords<br/> <%= text_field(:web_image, :keywords) %></td> </tr> <tr> <td colspan="2"> <td align="center"><%= submit_tag("UPLOAD")%></td> </tr> </table> <%= end_form_tag -%> -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Reasonably Related Threads
- Importing images with file_column over http?
- (resend - sorry!) Which controllers?
- Acts as attachement
- 4 commits - test/.gitignore test/image test/Makefile.am test/swfdec_test_function.c test/swfdec_test_image.c test/swfdec_test_image.h test/swfdec_test_initialize.as test/swfdec_test_initialize.h test/swfdec_test_test.c test/trace
- Problems with layouts