Rails skips model validation

for the life of me, i cannot get validates_presence_of to return any
errors, leaving the form blank and clicking submit fails on the create
or save method, but no validation errors are rendered.

http://pastie.caboo.se/98101

i should note that update_attributes is working

On 9/17/07, terry [email protected] wrote:

for the life of me, i cannot get validates_presence_of to return any
errors, leaving the form blank and clicking submit fails on the create
or save method, but no validation errors are rendered.

http://pastie.caboo.se/98101

You’re getting confused as to how errors are reported.

When you write

<%= error_messages_for :job %>

It looks for a model object in the instance variable @job (like how
form_for works). The error messages are stored in the model object by
a call to valid? (or save, create, etc.).

But you don’t have an @job variable, so error_messages_for doesn’t
find anything.

Your controller code is wrong. First, create() will return a Job model
object, even if it couldn’t be saved. So the “if” part is always true.

Second, you are doing a redirect after the create. But if you had an
error, your error messages would be lost due to the redirect.

This should work:

@job = current_user.jobs.build(params[:job])
if @job.save
flash[:notice] = “job has been posted”
redirect_to :action => ‘index’
return
end
render :action => ‘new’

Note the render instead of redirect at the end. If the save fails, you
need @job to be passed to the view, because it contains the error
messages.

HTH

wow, awesome. thank you bob

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs