Forum: Ruby on Rails Help with validation

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mat (Guest)
on 2006-02-16 15:33
I am trying to write some simple code post a form, update the database
and do some validation.  I have 2 problems:

1) The validation fails but I can't get hold of the error message
2) I can't get the form to redisplay the previously entered data for the
user to correct.

I have 2 controllers:

user and holiday.

A user can request a new holiday by filling in a form.  I have a
request_holiday method in the user controller.  My request_holiday.rhtml
sets up a form as follows:

<%= start_form_tag :controller => 'holiday', :action => 'new' %>

My holiday controller new method does the following:

[CODE]
def new
		@holiday = Holiday.new(@params[:holiday])
		if @holiday.save
			flash[:notice] = "Holiday requested."
			redirect_to(:action => "list")
		else
			flash[:notice] = "Failed."
			redirect_to(:controller => 'user', :action => "request_holiday")
		end
end
[/CODE]

In my holiday model, I check that the dates have been entered sensibly:

[CODE]
def validate
		if date_start > date_end
			errors.add_to_base("From date cannot be greater than to date")
		end
end
[/CODE]

I know the validation works because I can get the 'Failed' flash notice,
but I can't seem to get the error message using
error_message_for(:holiday) in the page.

I also can't seem to get the data to repopulate the form.  I normally
access the data in the controller using @params[:holiday] as the params
go into a hash called holiday in the params hash, but when they get
passed back to my form, they don't live in the holiday hash anymore and
are just normal parameters, e.g

[code]http://localhost:3000/user/request_holiday?date_st...]

My form uses tags such as

[CODE]<%= date_select("holiday", "date_end", :order => [:day, :month,
:year]) %>[/CODE]

I'm new to all this say maybe doing it completely wrong, can anyone
help/correct me?

Thanks
Ben M. (Guest)
on 2006-02-16 21:12
(Received via mailing list)
I hope someone answers you Mat! I've asked a similar question twice and
have seen someone
else post a similar question twice. The Agile book just doesn't seem to
give enough to go
on... where are the error messages stored? How does one access them?
Especially, if I have
a child object whose validate method has failed and prevented the save
of the parent,
where is /that/ error message?

b
This topic is locked and can not be replied to.