i have a person object. Persons don''t need to have addresses, but if
they have any address field value, they must have them all. So I have
something like this:
validates_presence_of :street_address, :city, :state, :postal_code
unless :address_blank?
address_blank? checks whether all of the address fields are blank.
If I run a test like this, it works:
describe "given attributes" do
before(:each) do
@valid_attributes = {
:first_name => "Jonny",
:middle_name => "D",
:last_name => "Miller",
:street_address => "123 This Street",
:street_address2 => "Suite 200",
:city => "Baltimore",
:state => "MD",
:postal_code => "23993",
:salutation => "Mr.",
:home_phone => "373-333-9999",
:cell_phone => "373-444-5555"
}
end
it "given attributes containing valid address values,
address_blank? should return false" do
it = Person.new(@valid_attributes)
it.address_blank?.should == false
end
However, if I do the following,
it "must have a street_address" do
@valid_attributes[:street_address] = ""
it = Person.new(@valid_attributes)
it.save
it.errors.on(:street_address).should == "can''t be
blank"
end
I get the following result.
''Person must have a street_address'' FAILED
expected: "can''t be blank",
got: nil (using ==)
So it appears that :address_blank? in the validates_presence_of is
evaluating to true.
Can anyone give me an idea of how I can detect why this is so?
I apologize in advance if this should be an rspec question, and will
take it there if advised as such.
best,
Tom
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
On Sat, Nov 1, 2008 at 5:29 AM, hoenth <hoenth-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> > i have a person object. Persons don''t need to have addresses, but if > they have any address field value, they must have them all. So I have > something like this: > > validates_presence_of :street_address, :city, :state, :postal_code > unless :address_blank?Instead of unless :address_blank? use :unless => :address_blank? since you need to pass options to validates_presence_of as opposed to writing a normal conditional. See the API docs for validates_presence_of for more details if you need them. Regards, Craig --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I must have looked at those API docs 10 times and didn''t see it. Would have bitten...and all that. Thanks for the quick response Craig. Worked like a charm. Tom On Nov 1, 9:05 am, "Craig Demyanovich" <cdemyanov...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Sat, Nov 1, 2008 at 5:29 AM, hoenth <hoe...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote: > > > i have a person object. Persons don''t need to have addresses, but if > > they have any address field value, they must have them all. So I have > > something like this: > > > validates_presence_of :street_address, :city, :state, :postal_code > > unless :address_blank? > > Instead of > > unless :address_blank? > > use > > :unless => :address_blank? > > since you need to pass options to validates_presence_of as opposed to > writing a normal conditional. See the API docs for validates_presence_of for > more details if you need them. > > Regards, > Craig--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---