# Validations should work, but aren't?

**URL:** <https://www.ruby-forum.com/t/validations-should-work-but-arent/178722>\
**Category:** Rails\
**Created:** [November 10, 2009, 3:08am UTC](https://www.ruby-forum.com/t/validations-should-work-but-arent/178722 "2009-11-10T03:08:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [November 10, 2009, 3:08am UTC](https://www.ruby-forum.com/t/validations-should-work-but-arent/178722/1 "2009-11-10T03:08:17Z")

</div>

Hi,

I’ve added a couple of validations to my model, however whenever I  
submit with no information (to purposely fail) in the view form, I just  
get a generic rails error message (the kind with all the stack trace  
information).

I have a feeling it might be in my controller, though I can’t seem to  
find out what to put to get it to pass to the model. Here’s the code in  
my controller:

def begin  
end

def end  
@location = Location.new(params[:location])  
end

Here’s the code in my begin view:

\<% form\_for :location, @location, :url =\> {:action =\> “end”} do |f| %\>

\<%= f.label :city1 “enter 1st location” %\>

\<%= f.text\_field :city1 %\>

\<%= f.label :city2, "enter 2nd location" %\>

\<%= f.text\_field :city2 %\>

\<% end %\>

And here’s the code to my end view:

\<%= @location.distance %\>

Model code:

validates\_presence\_of :city1, :city2

Any help would be appreciated. If there’s anything I missed, please let  
me know.

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [November 10, 2009, 3:48am UTC](https://www.ruby-forum.com/t/validations-should-work-but-arent/178722/2 "2009-11-10T03:48:50Z")

</div>

On Nov 9, 9:08 pm, Chris To [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I’ve added a couple of validations to my model, however whenever I  
> submit with no information (to purposely fail) in the view form, I just  
> get a generic rails error message (the kind with all the stack trace  
> information).

There’s nothing generic about a stack trace. Usually they pinpoint the  
problem precisely. Why don’t you post your stacktrace and give us  
something to go on other than “it doesn’t work”?

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [November 10, 2009, 3:52am UTC](https://www.ruby-forum.com/t/validations-should-work-but-arent/178722/3 "2009-11-10T03:52:55Z")

</div>

See in-line addition below:

Quoting Chris To [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):  
[snip]

> Here’s the code in my begin view:
> 
> \<% form\_for :location, @location, :url =\> {:action =\> “end”} do |f| %\>

\<%= f.error\_messages %\>

---

<div class="post-metadata">

**Author:** ![closedbook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/82dd89/32.png) [@closedbook](https://www.ruby-forum.com/u/closedbook)\
**Post date:** [November 10, 2009, 4:23am UTC](https://www.ruby-forum.com/t/validations-should-work-but-arent/178722/4 "2009-11-10T04:23:49Z")

</div>

Chris To wrote:

> Hi,
> 
> I’ve added a couple of validations to my model, however whenever I  
> submit with no information (to purposely fail) in the view form, I just  
> get a generic rails error message (the kind with all the stack trace  
> information).
> 
> I have a feeling it might be in my controller, though I can’t seem to  
> find out what to put to get it to pass to the model. Here’s the code in  
> my controller:
> 
> def begin  
> end
> 
> def end  
> @location = Location.new(params[:location])  
> end
> 
> Here’s the code in my begin view:
> 
> \<% form\_for :location, @location, :url =\> {:action =\> “end”} do |f| %\>
> 
> \<%= f.label :city1 “enter 1st location” %\>
> 
> \<%= f.text\_field :city1 %\>
> 
> \<%= f.label :city2, "enter 2nd location" %\>
> 
> \<%= f.text\_field :city2 %\>
> 
> \<% end %\>
> 
> And here’s the code to my end view:
> 
> \<%= @location.distance %\>
> 
> Model code:
> 
> validates\_presence\_of :city1, :city2
> 
> Any help would be appreciated. If there’s anything I missed, please let  
> me know.

Well, your ‘wiring’ is a little… unique.

From the api docs ([http://api.rubyonrails.org/](http://api.rubyonrails.org/)), for ActiveRecord::Base  
we have:

save(perform\_validation = true)

Saves the model.

If the model is new a record gets created in the database, otherwise the  
existing record gets updated.

If perform\_validation is true validations run. If any of them fail the  
action is cancelled and save returns false. If the flag is false  
validations are bypassed altogether. See ActiveRecord::Validations for  
more information.

Thereâ€˜s a series of callbacks associated with save. If any of the  
before\_\* callbacks return false the action is cancelled and save returns  
false. See ActiveRecord::Callbacks for further details.

Sooo… without attempting a save, the validations aren’t called by  
default. Otherwise how would you ever build a model without the  
potential for generating errors via validations due to fields that have  
not yet been set? In your case, you could call the validations manually  
with an @location.valid? if you don’t want to attempt the save, but why  
not let Rails do it automagically?
