Forum: Ruby on Rails Forms losing state on errors

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.
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 joevandyk (Guest)
on 2005-12-08 12:48
(Received via mailing list)
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
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 jeroen (Guest)
on 2005-12-08 12:56
(Received via mailing list)
Joe Van Dyk 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
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 joevandyk (Guest)
on 2005-12-08 13:49
(Received via mailing list)
On 12/8/05, Jeroen Houben <jeroen@terena.nl> 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?


<h1> Register For A Training Seminar</h1>

<%= start_form_tag %>
<b> Available Training events: </b> <br />

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

<br />

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

<br />
<p><%= submit_tag %>  </p>
<%= 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] << "<br />" +
@registration.errors.full_messages.join("<br />")
      end
    end
  end
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 joevandyk (Guest)
on 2005-12-08 13:53
(Received via mailing list)
On 12/8/05, Joe Van Dyk <joevandyk@gmail.com> 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).
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 jeroen (Guest)
on 2005-12-08 14:26
(Received via mailing list)
Joe Van Dyk 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
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 joevandyk (Guest)
on 2005-12-08 14:38
(Received via mailing list)
On 12/8/05, Jeroen Houben <jeroen@terena.nl> 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.  :-(  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.
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 joevandyk (Guest)
on 2005-12-08 14:46
(Received via mailing list)
On 12/8/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> > >>>>I'm writing a small application, where Users can Register for a
> > >>>
> 0.14.4.  :-(  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
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 jeroen (Guest)
on 2005-12-08 15:27
(Received via mailing list)
>
> 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
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 joevandyk (Guest)
on 2005-12-08 15:48
(Received via mailing list)
On 12/8/05, Jeroen Houben <jeroen@terena.nl> 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.
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 joevandyk (Guest)
on 2005-12-11 03:44
(Received via mailing list)
On 12/8/05, Joe Van Dyk <joevandyk@gmail.com> 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?
This topic is locked and can not be replied to.