Re: Validation without ActiveRecord

Carl Youngblood
Sun, 24 Jul 2005 04:39:04 -0000

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
>
>
> Carl Youngblood wrote:
> > Here is the controller code:
> >
> > def searchreq
> >   @req = SearchRequest.new if @req.nil?
> >     if @req.valid?
> >       # I haven''t put anything in here yet
> >     end
> > end
> >
> > Here is the model:
> >
> > class SearchRequest < ValidatingBase
> >   attr_accessor :fullname, :email, :phone
> >   validates_presence_of :fullname, :email, :phone
> > end
> >
> > In the form I''m just calling error_messages_for
''req''
> >
> > ValidatingBase comes from the following wiki page:
> >
> >
http://wiki.rubyonrails.com/rails/show/HowToUseValidationsWithoutExtendingActiveRecord
> >
> > So I''m actually not using ActiveRecord.
> >
> > Thanks for your help.
> >
> > Carl
> >
> > On 7/23/05, Wilson <defiler-ifvz4xmYPRU@public.gmane.org> wrote:
> >
> >>Can we see the controller code for the action?
> >>Usually it''s because you''re assuming you got
there from a fresh page.
> >>You need to make sure you don''t make duplicate AR objects
from "params"
> >>
> >>Carl Youngblood wrote:
> >>
> >>>Okay, now I have another problem with my validations.  Every
time I
> >>>submit the form it adds more errors to the list of validation
errors.
> >>>The list has tons of duplicate errors.  Any ideas as to what
might be
> >>>wrong?  I''m assuming that what it should do instead is
just keep
> >>>reloading the form with the same errors listed.
> >>>
> >>
> >>_______________________________________________
> >>Rails mailing list
> >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> >>http://lists.rubyonrails.org/mailman/listinfo/rails
> >>
> >
> > _______________________________________________
> > Rails mailing list
> > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>