Validation errors not displaying and will not save


#1

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 )%>

Image Title
<%= text_field(:web_image, :title) %>
  Image File <%= file_field(:uploader, :uploaded_file) %>

Upload another? <%= check_box(:uploader, :another?, {}, :true, :false)%>
Subtitle
<%= text_field(:web_image, :subtitle) %>
Keywords
<%= text_field(:web_image, :keywords) %>
<%= submit_tag("UPLOAD")%>
<%= end_form_tag -%>

#2

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