I have a formtastic form like the following:
= semantic_form_for @record, :remote => true, :id => 'my_form', :url => ...
Since I’m using :remote => true, I handle the response from the controller in javascript:
$('#my_form')
.bind("ajax:beforeSend", function(evt, xhr, settings){
})
.bind("ajax:success", function(evt, data, status, xhr){
$('#response').append(xhr.responseText);
})
.bind('ajax:complete', function(evt, xhr, status){
});
With :remote => false, my form automatically displays field validation errors resulting from trying to save the record in the controller like so:
@record = Record.new(params[:record])
@record.save
How do I make the formtastic field validation errors appear with :remote => true, where I’m handling the response myself in javascript?
Put the formtastic form in a view by itself.
In the controller action that handles the posted form, you can re-“render” the view when the record save fails. When the view is rendered, formtastic will look at the errors field in @record and display the validation errors. If the save succeeds, you can render an empty string to indicate success. (A more complex response scheme is possible depending on what you need.)
Then, in the client-side javascript, when the record fails to save, you can replace the existing form with the rendered view passed back from the controller.