I have a \'new\' form that gets validated in a post model. When the validator kicks in, it renders incorrectly.
The new post page path is at \'/posts/new\'
On va
If validation fails, user should see the form with the errors and stay at /posts/new
. That's what you want, right?
There's a simple way to achieve this.
Set remote: true
on the form to prevent the url from advancing. Handle ajax:success
to replace the form on the page with the newly rendered one.
$('form[data-remote=true]').on 'ajax:success', (e, data, status, xhr) ->
if isHTML(data)
thisForm = "form[id=#{@getAttribute('id')}]"
$(thisForm).replaceWith $(data).find(thisForm)
isHtml()
function is from this question.