Re: Validation without ActiveRecord

Wilson
Sun, 24 Jul 2005 16:55:27 -0000

You have a couple of options at least, and probably some more good ones
that I don''t know yet.
1. You could implement the "attributes=" and "attributes"
methods in
your model class.
2. Instead of writing @req.attributes = params[:req], you could write:
@req.property_name_1 = params[:req]["property_name_1"]
@req.property_name_2 = params[:req]["property_name_2"]
etc, etc, until you''ve gotten everything you need from the form.

I''m doing option #2 in an app I''m writing at the moment,
because I need
to build up a model from several forms before saving it.  attributes=
would overwrite the values that weren''t on the current form, so I split
it into three different ''update_whatever'' methods available to
the
controller.

If anyone knows a cooler way, let me know. Heh.

--Wilson.

Carl Youngblood wrote:
> I''m having trouble with the example code you provided.  @req
doesn''t
> have a member called "attributes".  Is this because it is not a
> descendent of ActiveRecord?  In this case, how do I populate my
> validating model with the post data?
>
> Thanks,
> Carl
>
> On 7/23/05, Wilson <defiler-ifvz4xmYPRU@public.gmane.org> wrote:
>
>>@req will always be nil when you get there, unless you''ve
stuffed it
>>into the session.
>>You want something like this:
>>
>>Assuming ''searchreq'' is the action that your form
POSTs to:
>>def searchreq
>>   @req = SearchRequest.new
>>   @req.attributes = params[:req]
>>   if @req.valid?
>>     # redirect somewhere, or do a flash[:notice] to the page, etc.
>>   else
>>     # flash an error, redirect somewhere, etc.
>>   end
>>end
>>