Forum: Ruby on Rails Validation errors not displaying and will not save

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
528bf58b969255ea594ca32dafcd996b?d=identicon&s=25 Decio Silva (decio)
on 2005-12-31 04:34
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">&nbsp;<!--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 -%>
528bf58b969255ea594ca32dafcd996b?d=identicon&s=25 Decio Silva (decio)
on 2005-12-31 18:20
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
> ---------------------
This topic is locked and can not be replied to.