Hello, I followed the instructions at http://wiki.rubyonrails.com/rails/show/HowToUseValidationsWithoutExtendingActiveRecord But now I''m having trouble trying to create my form view. Here''s a brief rundown of what I''ve done: - Created a class called SearchRequest that extends ValidatingBase and saved it in a file called search_request.rb and put it in my models directory. - Added a ''request'' method to my controller that instantiates SearchRequest and calls the valid? method - Created a view called request.rhtml and saved it into app/views/home. Even though my SearchRequest class has an accessor called fullname, I get a NoMethodError exception when I try to call <%= text_field :request, ''fullname'' %> It says: undefined method `fullname'' for #<ActionController::CgiRequest:0x24a6c14> I''m assuming that my controller somehow has not figured out that I want to use SearchRequest as my model. I tried adding it as a model to the controller but that didn''t fix this problem. Any advice would be greatly appreciated. Thanks, Carl
text_field requires that the first parameter be the string representation of an instance variable name. In your controller, you should call the "request" method and make an instance variable called "@request" with it. Then you can use <%= text_field ''request'', ''fullname'' %> (You may want to rename it, as well. The error message implies that there''s a potential name conflict.) --Wilson. Carl Youngblood wrote:> Hello, I followed the instructions at > > http://wiki.rubyonrails.com/rails/show/HowToUseValidationsWithoutExtendingActiveRecord > > But now I''m having trouble trying to create my form view. Here''s a > brief rundown of what I''ve done: > > - Created a class called SearchRequest that extends ValidatingBase and > saved it in a file called search_request.rb and put it in my models > directory. > - Added a ''request'' method to my controller that instantiates > SearchRequest and calls the valid? method > - Created a view called request.rhtml and saved it into app/views/home. > > Even though my SearchRequest class has an accessor called fullname, I > get a NoMethodError exception when I try to call > > <%= text_field :request, ''fullname'' %> > > It says: > > undefined method `fullname'' for #<ActionController::CgiRequest:0x24a6c14> > > I''m assuming that my controller somehow has not figured out that I > want to use SearchRequest as my model. I tried adding it as a model > to the controller but that didn''t fix this problem. > > Any advice would be greatly appreciated. >
On 7/22/05, Wilson <defiler-ifvz4xmYPRU@public.gmane.org> wrote:> text_field requires that the first parameter be the string > representation of an instance variable name. > In your controller, you should call the "request" method and make an > instance variable called "@request" with it. > Then you can use <%= text_field ''request'', ''fullname'' %> > > (You may want to rename it, as well. The error message implies that > there''s a potential name conflict.)Okay, I''m no longer getting an error, but now how do I make it automatically set up a flash message with the validation errors and highlight the problem fields? Thanks, Carl
Never mind, I think I figured it out. Sorry to trouble the list. On 7/23/05, Carl Youngblood <carl.youngblood-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 7/22/05, Wilson <defiler-ifvz4xmYPRU@public.gmane.org> wrote: > > text_field requires that the first parameter be the string > > representation of an instance variable name. > > In your controller, you should call the "request" method and make an > > instance variable called "@request" with it. > > Then you can use <%= text_field ''request'', ''fullname'' %> > > > > (You may want to rename it, as well. The error message implies that > > there''s a potential name conflict.) > > Okay, I''m no longer getting an error, but now how do I make it > automatically set up a flash message with the validation errors and > highlight the problem fields?
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. Thanks, Carl On 7/23/05, Carl Youngblood <carl.youngblood-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Never mind, I think I figured it out. Sorry to trouble the list. > > On 7/23/05, Carl Youngblood <carl.youngblood-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On 7/22/05, Wilson <defiler-ifvz4xmYPRU@public.gmane.org> wrote: > > > text_field requires that the first parameter be the string > > > representation of an instance variable name. > > > In your controller, you should call the "request" method and make an > > > instance variable called "@request" with it. > > > Then you can use <%= text_field ''request'', ''fullname'' %> > > > > > > (You may want to rename it, as well. The error message implies that > > > there''s a potential name conflict.) > > > > Okay, I''m no longer getting an error, but now how do I make it > > automatically set up a flash message with the validation errors and > > highlight the problem fields?
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. >
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 >
@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 >
attributes is not a member of my SearchRequest, I''m guessing because it is not descended from ActiveRecord. I just figured out that the reason the errors were getting repeated was because I set my controller''s model to be :search_request. After getting rid of that, the errors don''t get repeated, but when I try to accedd @req.attributes I get an error. One other nuisance I''m having is that my form text fields are not saving the values that I have already entered. 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 >
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 >
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 >>
Hi Carl, I''ve seen the "accumulating errors" problem during error_messages_for once before while helping someone on IRC to use ValidatingBase. In the end it was that he had put the ValidatingBase class in a file called "validatingbase.rb" rather than the (correct) "validating_base.rb". Regards, Trevor On 23-Jul-05, at 11:42 PM, 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