Pretty new at all this. I have a simple form for users to enter a couple pieces of information and then input their email address and push the submit button. I want to make it mandatory that they have to fill out their email address in order to push the submit button. If they don't fill out their email address they should get an error message on the email box that says the email can't be blank. I know this is super simple but I need exact help on where to put what code. I've been researching this all night and know that part of the code should go in the application_controller and other should go in the html file where the actual text_field:email is. I'd be grateful if someone could clearly tell me what the necessary steps are for doing this. Thanks!
+3
A:
It should go in your model. Add this:
class Model < ActiveRecord::Base
validates_presence_of :email
end
Check this link for more info: http://guides.rails.info/activerecord_validations_callbacks.html#validates-presence-of
Shripad K
2010-07-08 04:02:54
So I added the line to the file in model called search.rb: class Search < ActiveRecord::Base validates_presence_of :email end but then how do I add to my form? the current code is <%= f.text_field :email, :value => 'Please enter your email.', :onfocus => "if(this.value==this.defaultValue) this.value=''; this.style.color = '#333333'", :onblur => "if(this.value=='') this.value=this.defaultValue;this.style.color = '#aaaaaa'" %> what do i add in the view?
JNL
2010-07-08 05:11:10
Did you create a scaffold or you created the controller and views yourself? If its not automatically generated then you need to add a flash to your views: `<div><%= flash[:notice] %></div>` You can style the flash using CSS.
Shripad K
2010-07-08 05:33:40
Read more about flash in the api docs: http://api.rubyonrails.org/classes/ActionController/Flash.html
Shripad K
2010-07-08 05:36:01
that may have solved the problem! I'll look at the styling for that now. thanks very much.
JNL
2010-07-08 05:44:09
Sure. If you feel this answer solved your problem consider accepting it.
Shripad K
2010-07-08 05:55:33
one final question (i'm still working out the styling). but there is a page on our site called /searches. normally no one is supposed to go there and soon it will be password protected. but for now its open just no one knows about it. when a user inputs a bad email address - the site goes to /searches and shows the error message. how do i get it to show the error message and just go to the root / site?
JNL
2010-07-08 06:03:42
In your controller's action where it saves the record (Eg: `create` or `update` action) check the `redirect_to :action => "something"` or `redirect_to(@something)` tag and change it to: `redirect_to '/'` or `redirect_to :action => "index"` or whatever action that you have defined in your `map.root` in routes.rb
Shripad K
2010-07-08 06:09:40
+2
A:
In Rails 2, which I would assume you are using, validations go in the model. Which is located in $Rails_app_directory/app/model/$Classname.rb
In order to add ActiveRecord validations you can use the line
validates_presence_of :email_address
You should also consider using Rails to generate a confirmation field and filtering out ill-formatted email addresses. You could accomplish the former with:
validates_confirmation_of :email_address
with this, all you need to add to your form is a text_field for :email_address_confirmation
and the latter with a regular expression such as:
validates_format_of :email_address, :with => /\A[\w\.%\+\-]+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|jobs|museum)\z/i
nuclearsandwich
2010-07-08 04:06:20
So I added the middle two lines to the file in model called search.rb:class Search < ActiveRecord::Base validates_presence_of :email validates_confirmation_of :emailendbut then how do I add to my form? the current code is <%= f.text_field :email, :value => 'Please enter your email.', :onfocus => "if(this.value==this.defaultValue) this.value=''; this.style.color = '#333333'", :onblur => "if(this.value=='') this.value=this.defaultValue;this.style.color = '#aaaaaa'" %>
JNL
2010-07-08 04:54:02
And then are you saying I add the validates_format_of line to the model as well?
JNL
2010-07-08 04:54:20
As @nuclearsandwich says do add the validates_format_of line as well. You can test the regex at http://rubular.com to see if it validates your email addresses in the right manner. In fact i have set up a permalink of the same regex here: http://rubular.com/r/aNEL88CETK
Shripad K
2010-07-08 05:58:16
To add the confirmation. Simply add a new f.text_field :email_confirmation line to your form. Rails automatically creates the fieldname_confirmation parameter when you tell it to validate a confirmation. No need to add a confirmation row to your database or do anything but add a field for it. This way a missing email, a disparity between the email and its confirmation and a malformed email address will be three separate ActiveRecord errors.
nuclearsandwich
2010-07-08 06:27:17
A:
From snipplr, place in your model
validates_format_of :email,
:with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,
:message => 'email must be valid'
Schroedinger
2010-07-08 05:18:20
but then how do I connect that to my form where the email is being entered in the view? My form is:<%= f.text_field :email, :value => 'Please enter your email.', :onfocus => "if(this.value==this.defaultValue) this.value=''; this.style.color = '#333333'", :onblur => "if(this.value=='') this.value=this.defaultValue;this.style.color = '#aaaaaa'"%>
JNL
2010-07-08 05:29:18