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

Could you post the relevant code?

koloa wrote:

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' %>
<!--[form:temp]-->

<p><label for="jobs_company">Company</label><br/>
<%= text_field :temp, 'company' %></p>

<p><label for="jobs_description">Description</label><br/>
<%= text_area :temp, 'description', :cols => "30", :rows=> "10" %></p>
...etc...
<!--[eoform:temp]-->

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.

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

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...

koloa wrote:

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?

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' %>
<!--[form: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

<p><label for="item_company">Company</label><br/>
<%= text_field :item, 'company' %></p>

<p><label for="item_description">Description</label><br/>
<%= text_area :item, 'description', :cols => "30", :rows=> "10" %></p>
...etc...
<!--[eoform:temp]-->

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

koloa wrote:

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' %>
<!--[form: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

<p><label for="item_company">Company</label><br/>
<%= text_field :item, 'company' %></p>

<p><label for="item_description">Description</label><br/>
<%= text_area :item, 'description', :cols => "30", :rows=> "10" %></p>
...etc...
<!--[eoform:temp]-->

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

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.