Forms losing state on errors


#1

Hi,

I’m writing a small application, where Users can Register for a
TrainingEvent to be trained in a certain Role.

I have a form that contains radio boxes, one for every TrainingEvent.
I also have a select box, one for every Role. The user must enter in
a program number as well in this form.

When the user doesn’t fill in a piece of information (i.e. the program
number), I need to display the form again. However, the values that
the user entered for the TrainingEvent and the Role are reset to their
default values. How can I remember what the user selected?

Thanks,
Joe


#2

Joe Van D. wrote:

number), I need to display the form again. However, the values that
the user entered for the TrainingEvent and the Role are reset to their
default values. How can I remember what the user selected?

What does your view look like? Can you post it?

Jeroen


#3

On 12/8/05, Joe Van D. removed_email_address@domain.invalid wrote:

When the user doesn’t fill in a piece of information (i.e. the program
number), I need to display the form again. However, the values that
the user entered for the TrainingEvent and the Role are reset to their
default values. How can I remember what the user selected?

What does your view look like? Can you post it?

You can view the form at
http://joevandyk.com/cisv/register_for_training_seminar (login with
bob w/ password atest, or create a new user).


#4

On 12/8/05, Jeroen H. removed_email_address@domain.invalid wrote:

When the user doesn’t fill in a piece of information (i.e. the program
number), I need to display the form again. However, the values that
the user entered for the TrainingEvent and the Role are reset to their
default values. How can I remember what the user selected?

What does your view look like? Can you post it?

Register For A Training Seminar

<%= start_form_tag %>
Available Training events:

<% @available_training_events.each do |t| %> <% end %> <% @unavailable_training_events.each do |t| %> <% end %>
Location Hosting Chapter Registration Deadline Start Date End Date Spots Left
AVAILABLE TRAINING SEMINARS
<%= radio_button "registration", "training_event", t.id %> <%= t.location %> <%= link_to t.chapter.name, "" %> <%= t.registration_deadline.to_date %> <%= t.start_date.to_date %> <%= t.end_date.to_date %> <%= t.spots_left %>
UNAVAILABLE TRAINING SEMINARS -- don't register for these!
<%= t.location %> <%= link_to t.chapter.name, "" %> <%= t.registration_deadline.to_date %> <%= t.start_date.to_date %> <%= t.end_date.to_date %> <%= t.spots_left %>

<%= text_field_tag "registration[program_number]" %> Program Number

<%= select "registration", "role", Role.find(:all).collect { |r| [r.name, r.id] } %> Role


<%= submit_tag %>

<%= end_form_tag %>

And the controller:
def register_for_training_seminar
unless user?
store_location
redirect_to login_url
end

@registration = Registration.new
@available_training_events = TrainingEvent.available_training_events
@unavailable_training_events = TrainingEvent.find(:all) -

@available_training_events

if request.post?
  role = Role.find params[:registration][:role]
  training_event = TrainingEvent.find 

params[:registration][:training_event]
@registration = Registration.new :user => current_user,
:program_number =>
params[:registration][:program_number],
:role => role,
:training_event => training_event
if @registration.save
flash[:notice] = “Thanks for registering!”
redirect_to :action => ‘home’
else
flash[:errors] = “Problem with registration: "
flash[:errors] << “
” +
@registration.errors.full_messages.join(”
")
end
end
end


#5

Joe Van D. wrote:

You can view the form at
http://joevandyk.com/cisv/register_for_training_seminar (login with
bob w/ password atest, or create a new user).

That’s currently throwing an error.

Is it just happening after validation? Can yout strip out the validation
and the redirect and see what happens?

Jeroen


#6

On 12/8/05, Jeroen H. removed_email_address@domain.invalid wrote:

TrainingEvent to be trained in a certain Role.
What does your view look like? Can you post it?

You can view the form at
http://joevandyk.com/cisv/register_for_training_seminar (login with
bob w/ password atest, or create a new user).

That’s currently throwing an error.

Dang it, looks like the mysql timeout errors are still occuring on
0.14.4. :frowning: I’ll revert back to 0.14.3.

Is it just happening after validation? Can yout strip out the validation
and the redirect and see what happens?

It happens whenever the page is displayed again, so not just after
validation.


#7

Ok, reverted back to Rails 0.14.3, so http://joevandyk.com/cisv should
be working again.

Joe

I can see it now. The form looks ok to me. The code in the controller
looks a bit overly complex.

Can you put

<%= debug @registration %> in your form? This way you can easily tell
what values are assigned to the model.

I think if you have your assoications setup right, you should just be
able to do

@registration = Registration.new(params[:registration]

J


#8

On 12/8/05, Jeroen H. removed_email_address@domain.invalid wrote:

Can you put

<%= debug @registration %> in your form? This way you can easily tell
what values are assigned to the model.

I think if you have your assoications setup right, you should just be
able to do

@registration = Registration.new(params[:registration]

Currently, the relationships look like:
Registration belongs_to User
Registration belongs_to Role
Registration belongs_to TrainingEvent

TrainingEvent has_many Registrations
User has_many Registrations
Role has_many Registrations

And Registration also has other data, like program_number,
modified_on, created_on, roommate preference, etc.

So I don’t think I can just do a @registration =
Registration.new(params[:registration])… but I could be wrong. I’ll
try it out this evening.


#9

On 12/8/05, Joe Van D. removed_email_address@domain.invalid wrote:

Currently, the relationships look like:

So I don’t think I can just do a @registration =
Registration.new(params[:registration])… but I could be wrong. I’ll
try it out this evening.

Nope, I can’t.

Anyone have any ideas on how I can get the radio box selection to
persist if there’s a validation error?


#10

On 12/8/05, Joe Van D. removed_email_address@domain.invalid wrote:

I’m writing a small application, where Users can Register for a

0.14.4. :frowning: I’ll revert back to 0.14.3.

Is it just happening after validation? Can yout strip out the validation
and the redirect and see what happens?

It happens whenever the page is displayed again, so not just after validation.

Ok, reverted back to Rails 0.14.3, so http://joevandyk.com/cisv should
be working again.

Joe