views:

37

answers:

2

After validation, i got error and return back to :action => :new. Some field on form already filled, so i want to keep them filled afterr error message too. How it can be done?

Thanks.

A: 

In your view code for the 'new' action use FormHelper tags that reference the same model as your create action eg.

<%= text_field :user, :login %>

Then in your create action, use a model instance with the same name:

@user = User.new
# detect error, redirect to 'new'

The FormHelper will use the value of this instance if it is available.

jdeseno
If you want to retain information about what was inputted in the form, you can't redirect without storing the information somehow. The standard way to do it is to create `@user` in the create action and just `render :new`, which will pick up the changed values.
BaroqueBobcat
+1  A: 

Your View (new.html.erb) something like following

<%= error_message_for :user %>
<% form_for :user, :action=>"create" do|f|%>

<%= f.text_field :login %>

<% end %>

Controller Code (create method)

def create
  @user=User.new(params[:user])
  if @user.save
     redirect_to :action=>'index'
  else
     render :action=>'new'  #you should render to fill fields after error message
  end
end
Salil