# Validation isn't working - weird problem

**URL:** <https://www.ruby-forum.com/t/validation-isnt-working-weird-problem/109561>\
**Category:** Rails\
**Created:** [July 16, 2007, 9:48pm UTC](https://www.ruby-forum.com/t/validation-isnt-working-weird-problem/109561 "2007-07-16T21:48:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 16, 2007, 9:48pm UTC](https://www.ruby-forum.com/t/validation-isnt-working-weird-problem/109561/1 "2007-07-16T21:48:03Z")

</div>

I am really stuck, would appreciate some help from the community. I  
have a method in my controller like this:

def save\_business  
if(params[:id] == nil)  
@business = Business.new(params[:business])  
@business.user\_id = session[:user\_id]  
if @business.save  
flash[:notice] = ‘Business was successfully created.’  
redirect\_to :action =\> ‘edit\_business\_2’, :id =\> @business  
else  
render :action =\> ‘edit\_business\_1’  
end  
else  
@business = Business.find(params[:id])  
if @business.update\_attributes(params[:business])  
redirect\_to :action =\> ‘edit\_business\_2’, :id =\> @business  
else  
render :action =\> ‘edit\_business\_1’, :id =\> @business  
end  
end  
end

I have a form in my rhtml file like this:

\<% form\_for :business, :url =\> { :action =\> :save\_business, :id =\>  
@business} do |form| %\>

Legal name: \<%= form.text\_field :legal\_name, :size =\> 40 %\> (example: TFS, LLC)

... ... ... \<%= submit\_tag "Save", :class =\> "submit", :id =\> "yah", :style =\> "visibility:hidden; "%\> \<% end %\>

In the model file I have this:

validates\_presence\_of :legal\_name  
validates\_numericality\_of :zip  
protected  
def validate  
if legal\_name.blank?  
errors.add(:legal\_name, “should be at least 0.01”)  
end  
end

Neither of the validation is working, in the log file I get something  
like this:

Processing BusinessController#save\_business (for xxx.xx.xxx.xx at  
2007-07-16 14:03:02) [POST]  
Session ID: 35229c421c8a7df5d77aff8f7f2a5b3a  
Parameters: {“business”=\>{“city”=\>“”, “business\_description”=\>“”,  
“zip”=\>“hi”, “contact\_person”=\>“Sean”, “role\_at\_company”=\>“CEO”,  
“phone\_number”=\>“312-218-3413”, “street\_address”=\>“7a8”,  
“years\_business\_owned”=\>“2”, “entity\_type\_id”=\>“1”,  
“doing\_business\_as”=\>“sean28893”, “years\_in\_business”=\>“8”,  
“legal\_name”=\>“”}, “commit”=\>“Save”, “action”=\>“save\_business”,  
“id”=\>“9”, “controller”=\>“business”}  
Redirected to [http://www.sh21.com/business/edit\_business\_2/9](http://www.sh21.com/business/edit_business_2/9)  
Completed in 0.06194 (16 reqs/sec) | DB: 0.05713 (92%) | 302 Found  
[[http://www.sh21.com/business/save\_business/9](http://www.sh21.com/business/save_business/9)]

legal\_name is blank, and zip is not numeric, but they both save  
successfully…how weird!

Can anyone help me out?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 17, 2007, 12:35am UTC](https://www.ruby-forum.com/t/validation-isnt-working-weird-problem/109561/2 "2007-07-17T00:35:25Z")

</div>

Hi Sean,

# controller:

=============

> def save\_business  
> @business = Business.find( params[:id] ) || Business.new  
> @business.attributes = params[:business]

begin  
@business.save!  
flash[:notice] = params[:id] ?  
‘Business was successfully updated.’ :  
‘Business was successfully created.’  
redirect\_to :action =\> :edit\_business\_2, :id =\> @business

rescue ActiveRecord::RecordInvalid =\> error  
flash[:notice] = error.to\_s  
render :action =\> :edit\_business\_1, :id =\> @business

end

> end

# model:

========

# should do without extra method…

validates\_length\_of :legal\_name, :minimum =\> 1

# unless do you have floating point zip…

validates\_numericality\_of :zip, :only\_integer =\> true

Could not see any errors, but cleaned the code a little, hope this  
helps…

Aren’t there any ActiveRecord messages?

Regards  
Florian
