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">
开发者_如何学C <%= 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:
#users_controller.rb
def new
@user = User.new
end
def create
begin
@user = User.create!(params[:user])
rescue ActiveRecord::RecordInvalid => e
flash[:error] = e.record.errors.full_messages.to_sentence
render :action => "new"
end
end
then
#views/users/new.html.erb
<%= form_for(@user, :url => user_registration_path, :method=>'post', :id => 'user_new') do |f| %>
<%= f.label :password %>
<%= f.password_field %>
<%= f.text_field :email %>
# etc...
<% end %>
The difference here is using form_for
which takes a record, and using the slightly different f.text_field :email
instead of text_field_tag
helpers 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 @user
object 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).
精彩评论