Forum: Ruby on Rails Validations continued

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Craig W. (Guest)
on 2006-02-21 21:45
(Received via mailing list)
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
Nick S. (Guest)
on 2006-02-21 22:13
(Received via mailing list)
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
Craig W. (Guest)
on 2006-02-21 22:19
(Received via mailing list)
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
Nick S. (Guest)
on 2006-02-21 22:22
(Received via mailing list)
Whats the stacktrace? If it says the error is in your view code, then
thats where it is, and not in your controller.
Craig W. (Guest)
on 2006-02-21 22:49
(Received via mailing list)
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'  %></p>
23:
24: <p><label for="client_case_manager_wholename">Case
Manager</label><br/>
25: <%= options = [['Select a Case Manager', '']] +
@case_manager.collect {
26:  |cm| [cm.wholename, cm.id] }
27: select 'client', 'case_manager_id', options %></p>
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.
---
Nic W. (Guest)
on 2006-02-21 22:59
(Received via mailing list)
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. <removed_email_address@domain.invalid> 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
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
- Nic
Nick S. (Guest)
on 2006-02-21 22:59
(Received via mailing list)
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
Craig W. (Guest)
on 2006-02-21 22:59
(Received via mailing list)
I got it...I applied the answer you gave to Seth...

Thanks

Craig
Craig W. (Guest)
on 2006-02-21 23:08
(Received via mailing list)
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
This topic is locked and can not be replied to.