Keep form fields filled after an error (RoR) Keep form fields filled after an error (RoR) ruby ruby

Keep form fields filled after an error (RoR)


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  endend


Since in my case the form was in the view of another controller I did use flash to store my data and then check if there is data in flash present. If yes take this for default values for your input fields, if not just show whatever you want to show then.

So snippets from my code

flash[:date] = start_date# in the view where to form residesstart_day = flash[:date].nil? nil : flash[:date].day# ...<%= select day start_day ... %>

Hope that helps some of you ;-).