Tom Lieber on 6 Feb 2006 17:49:13 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PhillyOnRails] Simple Form Validation Example


On 2/5/06, Jason Lenhart <jplenhart@yahoo.com> wrote:
> I have a form being populated and have used the
> following:
>
> <%= text_field("user", "name", "class" => "field") %>
>
> Note that my user model class is not going to a
> database via scaffold - just a class in my models
> directory.
>
> When the form is submitted I cannot find a way to get
> a handle on the populated user model object.  I dug
> through my Rails book to no avail - did not see any
> straightforward ways of doing this.

Typically, in your controller, for the action to which the form is
submitted, you will have a line like this:

    @user = User.new(@params[:user])

When you write text_field("user", "name"), what you're saying is
"display a field for the parameter 'name' of the object @user". Thus,
by creating the object this way, the object @user will have the data
from the last form submission at all times. This is how, when you use
a scaffold, forms retain all of your entered values when there is an
error and the form is redisplayed. When there is no @user created in
this way, the form will be cleared after every request.

> Also - should I be declaring the instance varialbles
> in my user object any special way?

Instance variables in models can be used normally, and without being
too careful, you can avoid conflicting with any of the ActiveRecord
stuff.

> Thanks for any help that can be offered,
> Jason

Sincerely,

Tom Lieber
http://AllTom.com/
http://GadgetLife.org/
_______________________________________________
talk mailing list
talk@phillyonrails.org
http://lists.phillyonrails.org/mailman/listinfo/talk