Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep form fields filled after an error (RoR)

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

like image 202
There Are Four Lights Avatar asked May 26 '10 21:05

There Are Four Lights


1 Answers

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 
like image 130
Salil Avatar answered Sep 27 '22 22:09

Salil