Validations should work, but aren't?

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.

On Nov 9, 9:08 pm, Chris To [email protected] 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”?

See in-line addition below:

Quoting Chris To [email protected]:
[snip]

Here’s the code in my begin view:

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

<%= f.error_messages %>

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/), 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?