Validations continued

I simply can’t figure this out. I have been reading and re-reading Agile
book and wiki.rubyonrails.org - all sorts of validation methods and
still, it doesn’t work.

Controller code

def create
@client = Client.new(params[:client])
if @client.save!
flash[:notice] = ‘Client was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

Model # includes the following

validates_presence_of :first_name

protected
def validate
errors.add_on_empty %w(first_name)
end

And if I try to enter a new ‘client’ missing the first name, it sends me
to trace error page but doesn’t capture it as it suggests in Agile book.

This is making me crazy and is a continuation of my issue from last
night which a decent night sleep and breakfast has failed to clarify.

Help!

Craig

Your working to hard. Simply leave the validates_presence_of in and
get rid of the validate method.

The validate method is mainly used when you want to add some custom
validation that isn’t covered by the supplied validates_* methods.

Also, you want to use @client.save (no !). The ! is what is actually
sending out the exception, instead of just failing and adding the
errors to the object.

HtH!
-Nick

Whats the stacktrace? If it says the error is in your view code, then
thats where it is, and not in your controller.

OK - but following your instructions and I have been there many
times…I get sent to the trace error screen (not what I want) and it
states that there is an error in my ‘view’ code “_form.rhtml” on a
meaningless line.

Craig

Do you have a ‘Case Manager’? This error is occuring because it is
trying to load your object, and it is empty - a separate issue. Check
your development.log and see what is trying to load…

  • Nic.

On 2/21/06, Craig W. [email protected] wrote:

28:
Of course, if I have an entry in ‘first_name’ the new record is saved

def create

This is making me crazy and is a continuation of my issue from last
night which a decent night sleep and breakfast has failed to clarify.

Help!


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

  • Nic

Yep, you need to reload your @case_manager options in your action.
Instance variables aren’t saved across requests and need to be
repopulated. And as I stated in the other thread about this, render
just renders the action, it doesn’t actually call the action method.

So again, in your controller, before you call the render method to
redisplay the form you have to load the @case_manager variable with
something.

Try it. See if it works.
-Nick

I got it…I applied the answer you gave to Seth…

Thanks

Craig

Yes indeed - I thought that it would get that information from the ‘new’
method but it doesn’t so I had to explicitly put it there as you
suggested. Painful lesson.

Thanks

Craig

On Tue, 2006-02-21 at 15:21 -0500, Nick S. wrote:

Whats the stacktrace? If it says the error is in your view code, then
thats where it is, and not in your controller.


NoMethodError in Clients#create
Showing app/views/clients/_form.rhtml where line #25 raised:

You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.collect

Extracted source (around line #25):

22: <%= text_field ‘client’, ‘gov_id’ %>


23:
24:

Case
Manager

25: <%= options = [[‘Select a Case Manager’, ‘’]] +
@case_manager.collect {
26: |cm| [cm.wholename, cm.id] }
27: select ‘client’, ‘case_manager_id’, options %>


28:

As you can see from further down the page…I do have a
‘case_manager_id’…

Request
Parameters: {“commit”=>“Create”, “client”=>{“gov_id”=>“111”, “dob
(1i)”=>“1920”, “dob(2i)”=>“2”, “dob(3i)”=>“21”, “sex”=>“M”,
“case_manager_id”=>“13”, “middle_initial”=>"", “first_name”=>"",
“last_name”=>“test”}}

Of course, if I have an entry in ‘first_name’ the new record is saved
and all is happy - that’s why I expected validations to capture it.