When a user registers, if there is an error, after submitting the page refreshes and you see the error message about why the form wasn’t submitted to the DB.
But in this form, the values from the orig form are gone, yet they seem to be in memory bec if you click refresh you can resubmit, so the values are there.
Why isn’t rails showing the previously inputted values, allowing the user to update an resubmit?
My form for devise new.html.erb
<%= form_tag(user_registration_path, :method=>'post', :id => 'user_new') do |f| %>
.
.
<tr>
<td class="label">
<%= label_tag 'password', 'Password:', :id => 'lpassword', :for => 'password' %>
</td>
<td class="field">
<%= password_field_tag 'user[password]', nil, :id => 'user[password]', :maxlength => 50 %>
</td>
<td class="status"></td>
</tr>
<tr>
<td class="label">
<%= label_tag 'user[email]', 'Email Address:', :id => 'luser[email]', :for => 'user[email]' %>
</td>
<td class="field">
<%= text_field_tag 'user[email]', nil, :id => 'user[email]', :maxlength => 150 %>
</td>
<td class="status"></td>
</tr>
<tr>
<td class="label"><label id="lsignupsubmit" for="signupsubmit"> </label></td>
<td class="field" colspan="2">
<input id="signupsubmit" name="signup" type="submit" value="Sign Up" />
</td>
</tr>
</table>
<% end %>
The usual flow that handles this works like this:
then
The difference here is using
form_forwhich takes a record, and using the slightly differentf.text_field :emailinstead oftext_field_taghelpers which automatically sets the field value to@user.email. There’s a slightly different number of parameters, since you don’t need to tell those helpers what the field value should be, so check the docs on them.When the validation is hit, since the render is being called, and not redirect, the invalid
@userobject is still populated with the originally posted values, hence inserting them into the field.I’m not entirely sure what devise does in it’s internals, but this sort of best practices approach, should send you on the right way.
A not so clean alternative would be to set the values in the field from the params hash like so:
<%= text_field_tag('user[email]', (params[:user] ? params[:user][:email] : nil), :id => 'user-email', :maxlength => 150 %>. This method also assumes that a render, and not a redirect happens when an invalid form is posted (or that the params are sent through again in the redirect).