I have a contact form and I put the validation requirements in my contact model. When I submit the form how do I have that controller validate against the validation requirements set in the model? I am not putting the contact info from the form in the DB... I am just emailing it to myself using actionmailer which is working very well... very very well! Thanks for the help! John
On Fri, 11 Mar 2005 10:27:32 -0500, John Baku <john-CC0oh5EnFfVBDgjK7y7TUQ@public.gmane.org> wrote:> I have a contact form and I put the validation requirements in my contact model. > When I submit the form how do I have that controller validate against the > validation requirements set in the model?if you have stored the instance of your Contact-Model in @contact you can write: if @contact.valid? # send mail end
In my form I create all my input field using rails type fields ex) text_field "contact", "name" and then in my send_to controller I do what you recommended: if @contact.valid? and it tells me that the contact object is nil. So I decided to add: @contact = Contact.new into my controller that creates the contact form but since I have no table for Contact I get an error. What is the right way to go about this? Where is the contact object created to begin with? Even though I don''t put things into the DB should I create the DB structure for it so that I could use the Contact.new? Thanks for your feedback.> On Fri, 11 Mar 2005 10:27:32 -0500, John Baku <john-CC0oh5EnFfVBDgjK7y7TUQ@public.gmane.org> wrote: > > I have a contact form and I put the validation requirements in my contact > model. > > When I submit the form how do I have that controller validate against the > > validation requirements set in the model? > > if you have stored the instance of your Contact-Model in @contact you can > write: > > if @contact.valid? > # send mail > end > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> text_field "contact", "name" > > and then in my send_to controller I do what you recommended: > > if @contact.valid? > > and it tells me that the contact object is nil. > > So I decided to add: > > @contact = Contact.new > > into my controller that creates the contact form but since I have no table for > Contact I get an error.I thought you have a Contact-Model with the validations in it? Without an appropriate table, the Model will not work and so you can''t rely on the model-validations. A workaround would be to create the contacts-Table just to get your model working. You also could either do the validations on your own in the controller or write your own model (not inheriting from ActiveRecord::Base and so not relying on a database) which does the validations. I am not sure if there''s an easy way to make use of ActiveRecord::Errors in such a setup.
I have added the contact table to make things easier but I am still getting an undefined method for nil:NilClass. To give you more information: def index() @contact = Contact.new end def send_to() if @contact.valid? #send person and add to DB else render_action "index" end end And in my index.rhtml where the form is I have my input fields defined like: <%= text_field "contact", "name" %> Is the contact object created in the form and send to the send_to() or do I have to re-create it? I am a little confused. Thanks for the help.> > text_field "contact", "name" > > > > and then in my send_to controller I do what you recommended: > > > > if @contact.valid? > > > > and it tells me that the contact object is nil. > > > > So I decided to add: > > > > @contact = Contact.new > > > > into my controller that creates the contact form but since I have no table > for > > Contact I get an error. > > I thought you have a Contact-Model with the validations in it? Without > an appropriate table, the Model will not work and so you can''t rely on > the model-validations. A workaround would be to create the > contacts-Table just to get your model working. You also could either > do the validations on your own in the controller or write your own > model (not inheriting from ActiveRecord::Base and so not relying on a > database) which does the validations. I am not sure if there''s an easy > way to make use of ActiveRecord::Errors in such a setup. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> Is the contact object created in the form and send to the send_to() or do I have > to re-create it? >You redirect to another action, so you also have to create the object on the other action, just with the right params (the form only sends the information in @params to the other action): def send_to @contact = Contact.new(@params["contact"]) if @contact.valid? # ... end
Works like a charm.. thanks my friend. Just a for my information... in the controller that created the form what is the benefit of creating an object? Do I need to or only in my processing controller? Thanks> > Is the contact object created in the form and send to the send_to() or do I > have > > to re-create it? > > > You redirect to another action, so you also have to create the object > on the other action, just with the right params (the form only sends > the information in @params to the other action): > > def send_to > @contact = Contact.new(@params["contact"]) > if @contact.valid? > # ... > end > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> Just a for my information... in the controller that created the form what is the > benefit of creating an object? Do I need to or only in my processing > controller?You need the definition for several things: having default values, have an error-object (for example for the use with error_messages_for) and for having values inserted when the form is shown again because of validation errors. As you do the validation on the other controller the last one is not important in this case. So if you don''t need the default-values and don''t access the error-object or other functions associated with an ActiveRecord-Object, you could remove the definition in the index-controller! Just try it, you will see immediately, if it works or not! ;-)
> Just a for my information... in the controller that created the form what is the > benefit of creating an object? Do I need to or only in my processing > controller?Perhaps you mean the "action" that created the form? The benefit is two-fold: 1. You can (if desired) set certain default values for your form via the index action (in your case). Example: @contact.phone_number ||"(801)" # default area code 2. In your view you will have complete access to the methods of Contact that would normally be expected. e.g. @contact.errors, @contact.organization.company_name. For example, if you have a blank form that the user is submitting, and assuming they submit an incomplete form, your Contact model class will normally provide the @contact.errors object so that you can report to the user what parts of the form still need to be filled out. If you did not set @contact = Contact.new in your index action then even a simiple reference to @contact.errors in your view (say, index.rhtml) would throw a NoMethodError. Duane Johnson (canadaduane)
Makes perfect sense... now you make me wonder. How do I set default values? You also make it sound like it might be better to do the validation in the same controller. How would that work? Is that better? Quoting Michael Raidel <michael.raidel-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Just a for my information... in the controller that created the form what > is the > > benefit of creating an object? Do I need to or only in my processing > > controller? > > You need the definition for several things: having default values, > have an error-object (for example for the use with error_messages_for) > and for having values inserted when the form is shown again because of > validation errors. As you do the validation on the other controller > the last one is not important in this case. So if you don''t need the > default-values and don''t access the error-object or other functions > associated with an ActiveRecord-Object, you could remove the > definition in the index-controller! Just try it, you will see > immediately, if it works or not! ;-) > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Great explanation and thank you for introducing me to .errors. I already thought of a use for it that will come in handy. So this should work for setting default value according to your explanation. CONTROLLER: def index @contact = Contact.new @contact.website ||= "http://" end VIEW: <? = text_field "contact", "website" %> I don''t get any errors but it does not work. Any input on what I am doing wrong? Thanks again.> > Just a for my information... in the controller that created the form what > is the > > benefit of creating an object? Do I need to or only in my processing > > controller? > > Perhaps you mean the "action" that created the form? The benefit is > two-fold: > > 1. You can (if desired) set certain default values for your form via > the index action (in your case). Example: @contact.phone_number ||> "(801)" # default area code > > 2. In your view you will have complete access to the methods of > Contact that would normally be expected. e.g. @contact.errors, > @contact.organization.company_name. > > For example, if you have a blank form that the user is submitting, and > assuming they submit an incomplete form, your Contact model class will > normally provide the @contact.errors object so that you can report to > the user what parts of the form still need to be filled out. If you > did not set @contact = Contact.new in your index action then even a > simiple reference to @contact.errors in your view (say, index.rhtml) > would throw a NoMethodError. > > Duane Johnson > (canadaduane) > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> So I decided to add: > > @contact = Contact.new > > into my controller that creates the contact form but since I have no table for > Contact I get an error. > > What is the right way to go about this? Where is the contact object created to > begin with? Even though I don''t put things into the DB should I create the DB > structure for it so that I could use the Contact.new?It sounds like at this point you''re kind of adding stuff as it''s suggested, but without a good finished example to look at. I suggest you check out the excellent "Four Days on Rails" tutorial (http://rails.homelinux.org) that includes, among other topics, a brief discussion of form validation.
Is it possible that the "website" variable has a default value other than nil? If so, the ||= operator will not assign a new value. Its purpose is to set the value of a variable if and only if that variable is currently nil or false. You can just use "@contact.website = ''http://''" if you always want the variable to be set when the index page is viewed; however, the "standard" way to do it is to pass in the relevant @params to the constructor so that in the case of a form validation error, the user''s data will be used to fill the form back out with the same data they had used before submitting. e.g.: def index @contact = Contact.new(@params[''contact'']) @contact.website ||= "http://" end As long as the "website" variable is nil by default, it will be set to http:// unless the user filled something else in previously. Duane Johnson (canadaduane) On Fri, 11 Mar 2005 13:11:45 -0500, John Baku <john-CC0oh5EnFfVBDgjK7y7TUQ@public.gmane.org> wrote:> Great explanation and thank you for introducing me to .errors. I already > thought of a use for it that will come in handy. > > So this should work for setting default value according to your explanation. > > CONTROLLER: > > def index > @contact = Contact.new > @contact.website ||= "http://" > end > > VIEW: > > <? = text_field "contact", "website" %> > > I don''t get any errors but it does not work. Any input on what I am doing > wrong? > > Thanks again. > > > > > Just a for my information... in the controller that created the form what > > is the > > > benefit of creating an object? Do I need to or only in my processing > > > controller? > > > > Perhaps you mean the "action" that created the form? The benefit is > > two-fold: > > > > 1. You can (if desired) set certain default values for your form via > > the index action (in your case). Example: @contact.phone_number ||> > "(801)" # default area code > > > > 2. In your view you will have complete access to the methods of > > Contact that would normally be expected. e.g. @contact.errors, > > @contact.organization.company_name. > > > > For example, if you have a blank form that the user is submitting, and > > assuming they submit an incomplete form, your Contact model class will > > normally provide the @contact.errors object so that you can report to > > the user what parts of the form still need to be filled out. If you > > did not set @contact = Contact.new in your index action then even a > > simiple reference to @contact.errors in your view (say, index.rhtml) > > would throw a NoMethodError. > > > > Duane Johnson > > (canadaduane) > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >