I have a simple user registration page in my rails app that has two fields: name and email. I have client side validation on each text field and server side for the submit button (This is the recommended way right?)
My problem is that I don’t know how to repopulate the two fields with the previous values if they submitted it with a mistake. I have tried a few days such as:
format.html { redirect_to :back, :notice => 'Uh Oh! Something went wrong!', :name => @user.name } <– create action
and
if(params.has_key?(:name)) <– new action
@user.name = params[:name]
end
Any help appreciated!
Yes this is the recommended way to put validation on both client and server side.
Regarding retaining the fields is concerned on redirect the @ variables use to be get destroyed only flash persist up to one redirect.
Either u can pass your validations message in flash which is bad way the good one is to render the form page (good to be make form as seperate partial) and the @ object will be automaticaly map the relevant fields like name, email.
Hope this will help you.
thanks