Validates_presence_of not working...could it be my partial?

hi,
in my controller i am returning an instance of my model to a form. this
form uses a partial for contact information. the fields in the form are
also part on my model.

but when i add validates_presence_of :name to my model, the error
message does not appear when i submit the form with the name field
blank.

could this be a problem with my partial? or in my db, must the default
values for fields be null? i put an if check that checks if the field is
empty and that returns true.

Could you post the relevant code?

mycontr…

def form
@cat = getMainCatName(params[:fid])
getForm(@cat)
end

application.rb…

def getForm(category)
case category
when “re”
@temp = House.new
@ddvd_cover = Repicture.new
@cat = ‘re’
when “job”
@temp = Job.new
@cat = ‘job’
end
render :template => “displays/form”
end

form.rhtml
<% if @cat == “job”%>
<%= start_form_tag :action => ‘create’, :category => ‘job’%>
<%= render :partial => ‘displays/fjob’ %>
<%= submit_tag “Create”%>
<%= end_form_tag%>
<%end%>

_fjob.rhtml

<%= error_messages_for ‘temp’ %>

Company
<%= text_field :temp, 'company' %>

Description
<%= text_area :temp, 'description', :cols => "30", :rows=> "10" %>

...etc...

modelJob
class Job < ActiveRecord::Base
validates_presence_of : company, : description, :on => :create,
:message => “can’t be blank”
end

thanks for any help, been kind of stuck on this all day.

ive been reading and it seems that my problem may be the redirect?
anyway around this?

koloa wrote:

def getForm(category)
end

<%= text_area :temp, ‘description’, :cols => “30”, :rows=> “10” %>

thanks for any help, been kind of stuck on this all day.

What happens if you try to create an instance of Jon in the console. Try
doing this (in the console):
j = Job.new
j.save
p j.errors

You should see whether the validation is working – as j.errors will
have a bundle of errors (not sure, by the way that it is allowed to have
a space between the colon and the rest of the symbol – would have
thought you wanted

validates_presence_of :company, :description

HTH

hi chris thanks for the suggestion. i just tried it and it seems that
the error condition is coming out correct. console reports that my
validates error flags are raised when my text field is nil.

so it must have to do with my redirect?

koloa wrote:

hi chris thanks for the suggestion. i just tried it and it seems that
the error condition is coming out correct. console reports that my
validates error flags are raised when my text field is nil.

so it must have to do with my redirect?

Don’t know – don’t think you posted the create action, which is I guess
where this was…

do i need any modifications to my css file?

hi
def create
@job = Job.new(params[:temp])
if @job.save
flash[:notice] = ‘Profile was successfully created.’
redirect_to :action => ‘index’
else
render :text => “EMPTY>>???”
#render :action => ‘new’
end
end

ok, it seems that it is now getting to the empty statement, my only
problem now is that how do i redirect this action to redisplay the form.

the form.rhtml is in a directory called displays. in the form file, i
have case stament that calls the partial for the type of form. so i have
a partial for cars, jobs, food, etc…

when i tried the render option, it looks for form in my controller
folder.

can the validate be done like this?

koloa wrote:

end
when i tried the render option, it looks for form in my controller
folder.

can the validate be done like this?

I think part of the problem is you’re trying to do things the non-Rails
way – which can be done but it does make things more difficult.
There are two idiomatic Rails ways of doing something like this – the
new 1.2 REST-ful way, and the old verb-based action way. Either way,
Rails works much of its magic by assumptions on your naming convetions
– you can override them, but it’s so much easier if you don’t have to.

Assuming you haven’t got into the RESTful goodness yet – and it’s worth
looking at), I would suggest something like the following (not tested,
and I’ve made some possibly incorrect assumptions about your models):

AllPurposeController < ApplicationController

def new
@item = params[:fid].constantize.new #instantiate new item of class
params[:fid], e.g. “Job”
@ddvd_cover = Repicture.new if params[:fid] == “House”
end
end

def create
@item = params[:fid].constantize.new(params[:item])
if @item.save
flash[:notice] = ‘Profile was successfully created.’
redirect_to :action => ‘index’
else
render :action => ‘new’
end
end

form.rhtml

<%= error_messages_for ‘item’ %>

<%= start_form_tag :action => ‘create’, :fid => @item.class.to_s %>

put stuff here that’s common to all items

<%= render :partial => ‘displays/#{@item.class.to_s.downcase}_form’ %>

can your form_partials “job_form”, “house_form”, etc

<%= submit_tag “Create”%>
<%= end_form_tag%>

_job_form.rhtml

Company
<%= text_field :item, 'company' %>

Description
<%= text_area :item, 'description', :cols => "30", :rows=> "10" %>

...etc...

modelJob
class Job < ActiveRecord::Base
validates_presence_of : company, : description, :on => :create,
:message => “can’t be blank”
end

koloa wrote:

– you can override them, but it’s so much easier if you don’t have to.
@ddvd_cover = Repicture.new if params[:fid] == “House”
end

can your form_partials “job_form”, “house_form”, etc

Description

No prob. I came from PHP background so I know the feeling. One tip:
start testing (either Test First or Test Driven) as soon as poss – it
took me a while to catch on to it, but now it’s a joy to refactor apps
because it.

wow, chris, thank you so much for much detail response. man, everytime i
learn more about ruby rails, im always shaving off more and more code.
after i completed my first application, i noticed i can reduce the code
by 80% doing more OO. after i did that, i looked at your code, and
notice by using constantize i can trim probably another 20%. hotdamn,
before i know it, my application will be about 10% the size as when i
first started coding the application!

thanks again.

Chris T wrote:

koloa wrote:

end
when i tried the render option, it looks for form in my controller
folder.

can the validate be done like this?

I think part of the problem is you’re trying to do things the non-Rails
way – which can be done but it does make things more difficult.
There are two idiomatic Rails ways of doing something like this – the
new 1.2 REST-ful way, and the old verb-based action way. Either way,
Rails works much of its magic by assumptions on your naming convetions
– you can override them, but it’s so much easier if you don’t have to.

Assuming you haven’t got into the RESTful goodness yet – and it’s worth
looking at), I would suggest something like the following (not tested,
and I’ve made some possibly incorrect assumptions about your models):

AllPurposeController < ApplicationController

def new
@item = params[:fid].constantize.new #instantiate new item of class
params[:fid], e.g. “Job”
@ddvd_cover = Repicture.new if params[:fid] == “House”
end
end

def create
@item = params[:fid].constantize.new(params[:item])
if @item.save
flash[:notice] = ‘Profile was successfully created.’
redirect_to :action => ‘index’
else
render :action => ‘new’
end
end

form.rhtml

<%= error_messages_for ‘item’ %>

<%= start_form_tag :action => ‘create’, :fid => @item.class.to_s %>

put stuff here that’s common to all items

<%= render :partial => ‘displays/#{@item.class.to_s.downcase}_form’ %>

can your form_partials “job_form”, “house_form”, etc

<%= submit_tag “Create”%>
<%= end_form_tag%>

_job_form.rhtml

Company
<%= text_field :item, 'company' %>

Description
<%= text_area :item, 'description', :cols => "30", :rows=> "10" %>

...etc...

modelJob
class Job < ActiveRecord::Base
validates_presence_of : company, : description, :on => :create,
:message => “can’t be blank”
end