I have a form that can be used both remotely and normally.
= form_for @comment, html: { class: 'comment-form' }, remote: request.xhr? do |f|
= f.text_area :body
= f.submit
I want the form to submit only if the body
textarea has content:
$(document).on 'submit', '.comment-form', (e) ->
text = $(this).find('#comment_body').val()
false if text.length < 1
That code works when the form is not Ajax. But when it's remote, it fails and the form still submits. Why? I also tried:
if text.length < 1
e.preventDefault()
false
I'm using Rails 4 with Turbolinks.
Update: I tried binding the ajax:beforeSend
event, it still submits:
$(document).on 'page:load', '.comment-form', ->
$(this).bind 'ajax:beforeSend', ->
alert "hi"
I see no alert...