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.
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.
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.
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