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 Development, 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 ?
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.
> 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.
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.
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...
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.
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.
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.