How to maintain Form State between Postback?

Hi all,

I’ve trying to look for answers for my problem but so far couldn’t
find one, hopefully you can tell me what to do.
This problem is so ubiquitous to Web D., pertaining the fact
that the web is stateless, i.e. how to maintain form’s state between
postback.
Basically, I have a form with some validations run for some of the
fields, whenever the user submit the form and the one of the
validations failed, the page will reload with the error message
shown. The problem is, all the fields, that were previously filled-in
are all empty again, which means the user would have to enter all the
information all over again. This is obviously not ideal scenario from
the user interaction point of view.

Now, is there anything in Rails that can help me to maintain the state
automatically ? Have I missed anything ?

Thanks for your help,
Chris

On 12 Oct 2007, at 11:35, mahadewa wrote:

automatically ? Have I missed anything ?

The standard pattern if that upon validation failing you don’t
redirect, you just render the form for the creation/editing action.

Fred

The standard pattern if that upon validation failing you don’t
redirect, you just render the form for the creation/editing action.

Fred

And this will maintain the state of the fields ? How ?
Edit will need an existing data, which is not the case here.

Thanks

I think I understand this now.

Thanks for the help

Now, is there anything in Rails that can help me to maintain the state
automatically ? Have I missed anything ?

You’ve found the answer evidentially (I’m afraid to ask what it was),
but to all the others that ask questions like this, please (please!)
get a Rails book or tutorial and work through an example so you can
get at least a minimal understanding of how the framework is supposed
to be used.

I know you mean well, Bob, but let’s extend the leash a little. Even
people who’ve read books and tutorials can have basic questions some
times. That’s okay. It’s time to intervene if a person is constantly
blasting the mailing list with question after question that could all
be answered with a minimum of due diligence, but asking for help a few
times shouldn’t be a crime.

On 10/12/07, mahadewa [email protected] wrote:

Now, is there anything in Rails that can help me to maintain the state
automatically ? Have I missed anything ?

You’ve found the answer evidentially (I’m afraid to ask what it was),
but to all the others that ask questions like this, please (please!)
get a Rails book or tutorial and work through an example so you can
get at least a minimal understanding of how the framework is supposed
to be used.

Bob,

Just to say that I do have the book and read it. But what I’m doing
at the moment is a bit more complicated than the example in the book.
Normally I wouldn’t ask this kind of question, because the framework
should work the way it does without much intervention.
This time I have to two models on the same form, and I am also doing
custom validation outside the Model.

Infact, even now that I understand about the postback pattern, my View
is ‘still’ not loading the form’s data to the field on postback.
Maybe that means I still don’t understand then, who knows.

Cheers,
Chris

On 10/12/07, mahadewa [email protected] wrote:

Bob,

Just to say that I do have the book and read it. But what I’m doing
at the moment is a bit more complicated than the example in the book.
Normally I wouldn’t ask this kind of question, because the framework
should work the way it does without much intervention.
This time I have to two models on the same form, and I am also doing
custom validation outside the Model.

Sorry, didn’t mean to insult you. None of that info was in the OP.

Infact, even now that I understand about the postback pattern, my View
is ‘still’ not loading the form’s data to the field on postback.
Maybe that means I still don’t understand then, who knows.

Well, without seeing any code, I don’t know how anyone can help beyond
just guessing…

Cheers.

Hi All

I just popped into the forum looking for precisely the same thing as
Mahadewa. I do have a few books and I’m working my way through them,
but if anyone can point me in the right direction with a link or a
quick paste of code I would be very grateful.

Thanks
Gavin

On 10/12/07, DHH [email protected] wrote:

I know you mean well, Bob, but let’s extend the leash a little. Even
people who’ve read books and tutorials can have basic questions some
times. That’s okay. It’s time to intervene if a person is constantly
blasting the mailing list with question after question that could all
be answered with a minimum of due diligence, but asking for help a few
times shouldn’t be a crime.

Sorry, didn’t mean to imply it was a crime.

Here is my controller’s code:

=======
def new

@user = User.new
@code = params[:invite_code]

if params[:invite_code].nil?
  @invite = Invite.new
else
  @invite = Invite.find_by_invite_code(params[:invite_code])
end

end

def create

if params[:invite][:invite_code].empty?
  flash[:error] = "You need the invite code"
  render :action => 'new'
  return
end

@code = params[:invite][:invite_code]

if params[:user][:email].empty?
  flash[:error] = "Please enter your email address"
  render :action => 'new'
  return
end

@user = User.new(params[:user])
flash[:error] = nil

if @user.save!
  self.current_user = @user
  redirect_back_or_default('/')
  flash[:notice] = "Thanks for signing up!"
end

rescue ActiveRecord::RecordInvalid
render :action => ‘new’

end

And here is my view:

Sign Up
<% form_for :user, @user, :url => users_path do |f| -%>

<div class="form_item">
  <label for="invite_code">Enter your Invitation Code</label><br />
  <%= text_field :invite, :invite_code %>

</div>

<fieldset>
  <legend>Personal Details</legend>
  <div class="form_item">
    <label for="username">Username</label><br />
    <%= f.text_field :username, :class => 'input_text' %>
    <%= error_for_field :user, :username %>
  </div>
  <div class="form_item">
    <label for="email">Email</label><br/>
    <%= f.text_field :email, :class => 'input_text' %>
    <%= (error_for_field :user, :email) %>
  </div>
  <div class="form_item">
    <label for="password">Password</label><br/>
    <%= f.password_field :password, :class => 'input_text' %>
    <%= (error_for_field :user, :password) %>
  </div>

  <div class="form_item">
    <label for="password_confirmation">Confirm Password</label><br/>
    <%= f.password_field :password_confirmation, :class =>

‘input_text’ %>
<%= (error_for_field :user, :password_confirmation) %>

  <div class="form_item">
    <label for="first_name">First Name</label><br/>
    <%= f.text_field :first_name, :class => 'input_text' %>
  </div>

  <div class="form_item">
    <label for="last_name">Last Name</label><br/>
    <%= f.text_field :last_name, :class => 'input_text' %>
  </div>

  <div class="form_item">
    <label for="birthday">The day you were born</label><br/>
    <%= f.date_select :birthday, :start_year => 1945, :end_year =>

Time.now.year,
:order => [:day,:month,:year], :include_blank => true, :class =>
‘input_select’ %>
<%= error_for_field :user, :birthday %>

  <div class="form_item">
    <label for="gender">Gender</label><br/>
    <%= f.select :gender, [["Male", "M"], ["Female", "F"]],

{ :include_blank => true }, :class => ‘input_select’ %>

</fieldset>

<div class="form_item">
  <%= submit_tag 'Sign up', :class => 'input_button' %>
</div>
<% end -%>

Any help would be much appreciated.

Thanks !

Ok, I found a solution. I don’t know if this is the best one, but it
works for me. I’m sure somebody will be able to come up with a better
solution.

So, here is what I did.

In my View I put, for example:

<%= text_field :invite, :invite_code, :value => @code, :class =>
‘input_text’ %>

The important part is the hash :value => @code

and then in my controller, I put:

@code = params[:invite][:invite_code]

This will persist the value of the above field on postback.

Hope this helps,
Chris

I have to interject here, sorry this is such an old post but no one has
solved this in a manner satisfying for me so I did something about it.
Form state still remains an issue across all platforms.

I just released a jQuery plugin running off of HTML 5 offline storage
and it works in all the major browsers to maintain your form state, just
drop one line of code and you can do it based on a browser session or
persist it across sessions with no server-side programming needed or
cookies. It can even work on an HTML page. I have done development for
PHP, .NET, Ruby, Python and JSP so I tend to see JavaScript as a godsend
and a preferable solution because I can reuse it everywhere. HTML 5
rocks.

I’m not making money off of this as its 100% free but I feel that its my
duty to let people know.

Here is the plugin link → http://www.jasonsebring.com/dumbFormState

On 10/13/07, mahadewa [email protected] wrote:

  return
@user = User.new(params[:user])

end

Hi mahadewa,

Validations are usually better off in the model than the controller.

For the sake of understanding what’s going on, however, the form
elements are prepopulated with your model data when you use the form
builder methods in your views. For example, when you do:

<%= f.text_field :last_name, … %>

It will generate an HTML tag with the value attribute set to
@user.last_name. I’m guessing that it just wasn’t prepopulated when
one of those controller validations before the @user = … line
failed. Since you `return’ed from your controller action, @user was
left unset, and the view had nothing to prepopulate from. If you move
the @user = … line to before the validations, it’d probably work.

But again, validations are usually best done in the model.

Regards,
George.