Topic: Validations not working?

I am trying to put validations on my links model (simple crud for creating a link).

class Link < ActiveRecord::Base
  belongs_to :category
  validates_presence_of :title, :on => :create, :message => "can't be blank"
  validates_presence_of :description, :on => :create, :message => "can't be blank"
  validates_presence_of :name, :on => :create, :message => "can't be blank"
  validates_presence_of :email, :on => :create, :message => "can't be blank"
  validates_presence_of :category, :on => :create, :message => "can't be blank"
  validates_format_of :url, :with => /^(https?:\/\/)?([^\/]+)(\/.+)?$/i
end

And if I go to http://localhost:3000/links/new

and just click "create" i get a new link or just errors from nothing being put in. So.... why is this not working? Lol.

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.

Re: Validations not working?

I actually get this if it's blank =>

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.each

Extracted source (around line #21):

18:
19: <p><label for="link_category">Category</label><br/>
20: <select name="link[category_id]">
21: <% @categories.each do |category| %>
22: <option value="<%= category.id %>">
23:     <%= category.title %>
24: </option>

Last edited by DFischer (2006-12-28 01:09:40)

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.

Re: Validations not working?

It looks like @categories doesn't exist in your view.  Are you setting @categories in your controller?

Re: Validations not working?

Ahh, that was the mistake. I put @category = Category.find_all in "new" instead of "create".

Thanks!

Other question though, why isn't my URL validation working?

If I type "asd" there is no error on create.

This is my model

class Link < ActiveRecord::Base
  belongs_to :category
  validates_presence_of :title, :on => :create, :message => "can't be blank"
  validates_presence_of :description, :on => :create, :message => "can't be blank"
  validates_presence_of :name, :on => :create, :message => "can't be blank"
  validates_presence_of :email, :on => :create, :message => "can't be blank"
  validates_presence_of :category, :on => :create, :message => "can't be blank"
  validates_format_of :url, :with => /^(https?:\/\/)?([^\/]+)(\/.+)?$/i
end

I want it to be able to require a domain.com structure.

Last edited by DFischer (2006-12-28 05:02:21)

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.

Re: Validations not working?

That's a bit harder to answer.  With Regexps it's all a matter of trial and error.  I'd recommend going into irb (just type irb at a command line if you've got it installed) and playing around with things like:

'asdf'.match /^(https?:\/\/)?([^\/]+)(\/.+)?$/i

that way you can see if it's the regular expression or some other thing that's causing the validation to not work.

Re: Validations not working?

another reason could be that you dont have the the correct call in the controller.  You have to use render :action => xxx if you want validation errors to show, not redirect_to :action => xxx.

Also, you need the errors_for :object method in your view so that it shows the validation error.

I dont know if you have this from the code shown, but these are the reasons i get when validations dont work.


--jake




DFischer wrote:

Ahh, that was the mistake. I put @category = Category.find_all in "new" instead of "create".

Thanks!

Other question though, why isn't my URL validation working?

If I type "asd" there is no error on create.

This is my model

class Link < ActiveRecord::Base
  belongs_to :category
  validates_presence_of :title, :on => :create, :message => "can't be blank"
  validates_presence_of :description, :on => :create, :message => "can't be blank"
  validates_presence_of :name, :on => :create, :message => "can't be blank"
  validates_presence_of :email, :on => :create, :message => "can't be blank"
  validates_presence_of :category, :on => :create, :message => "can't be blank"
  validates_format_of :url, :with => /^(https?:\/\/)?([^\/]+)(\/.+)?$/i
end

I want it to be able to require a domain.com structure.

---------------------------------------------------------------------
Rails Development - Agile rails consulting and development for startups on a budget
Flvorful Blog - Some ramblings
Flvorful's Open Source Projects - Some handy hacks for your rails projects.

Re: Validations not working?

BTW, validates_presence_of takes multiple attributes, so you can remove a little duplication this way:

validates_presence_of :title, :description, :name, :email, :category, :on => :create, :message => "can't be blank"

Best to get the other problem working before trying this though.

Railscasts - Free Ruby on Rails Screencasts