I have a form_with
that calls a controller that responds in JSON. I would like to run javascript (handle errors, ...) during an event ajax:success
or ajax:error
.
I followed the Rails documentation. Unfortunately, the variables in my events are undefined
. Only e
are set.
Form:
<%= form_with url: contact_path, scope: 'message', id: 'new_message' do |f| %>
<div class="row">
<div class="input-field col s6">
<%= f.text_field :name, class: 'validate' %>
<%= f.label :name, 'Nom' %>
</div>
<div class="input-field col s6">
<%= f.email_field :email, class: 'validate' %>
<%= f.label :email, 'Email' %>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<%= f.telephone_field :phone, class: 'validate' %>
<%= f.label :phone, 'Téléphone' %>
</div>
<div class="input-field col s6">
<%= f.text_field :website, class: 'validate' %>
<%= f.label :website, 'Site Web' %>
</div>
</div>
<div class="row">
<div class="input-field col s12">
<%= f.text_area :content, class: 'materialize-textarea validate' %>
<%= f.label :content, 'Message' %>
</div>
</div>
<div class="row">
<div class="col s6">
<%= recaptcha_tags %>
</div>
<div class="col s6">
<%= button_tag class: 'btn primary waves-effect waves-light right' do %>
Envoyer
<i class="material-icons right">send</i>
<% end %>
</div>
</div>
<% end %>
Controller:
def create
@message = Message.new(message_params)
respond_to do |format|
if verify_recaptcha(model: @message) && @message.valid?
MessageMailer.new_message(@message).deliver
format.json { render json: @message, status: :created }
else
format.json { render json: @message.errors, status: :unprocessable_entity }
end
end
end
CoffeeScript:
$(document).ready ->
$("#new_message").on("ajax:success", (e, data, status, xhr) ->
console.log(xhr);
).on "ajax:error", (e, xhr, status, error) ->
console.log(xhr);
Result:
undefined message.self-5eda06948d26dade035273a7f168828c2cb1852e26937c49f50da7ca2532ad72.js:4:14
Started POST "/contact" for 127.0.0.1 at 2017-06-13 17:19:17 +0200
Processing by MessagesController#create as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"aBxh+eJedXw0WifdUXFAlgH0gWxNpiDQa2J3hdJayC/wBaccIxNSVK4pycYwu9XMb1s2/r9S8+FteRFoocdrZQ==", "messages"=>{"name"=>"", "email"=>"", "phone"=>"", "website"=>"", "content"=>""}, "g-recaptcha-response"=>"03AOPBWq9SEfWUxzdyMRMdd0p65vonbGjNjA_mhCA07vfoXyA6_nEVn220Ot5HU90_KpQv6ziii0VCoa1hZosUcsARL7DuJbFHA2kt_NBHvnURmPpKR0SMq0OfulzJokvobdu9t1z33Uby3oTAtTgEAEmxX6yDc_yuwmqHXAjh5SL0bhAyD6AF2wAN8me7fMKMULu_DS5KtLgwJjK8B2TmvGydn0neHDWcfSbDKP7WRP-RQQePc9hDcOCbUFoVrW2kfpg3p4OV-hWiV4q6-HBRZK3A1mcj5rJZd_zBe3v-yx6LHAXB2Bsb9Uc", "button"=>""}
Completed 422 Unprocessable Entity in 2701ms (Views: 0.5ms | ActiveRecord: 856.3ms)
contact_path
? you wantmessage_path
or not? – Utileundefined
I use another strategy that is to respond in javascript to work around the problem. (github.com/mohitjain/ajaxified_scaffold/blob/master/app/views/…) – Idalinerails server
) – Utilemessage
, maybe because all message parameters are blank. You have validator toMessage
? or database restriction in our messages table? . You can debug and put abyebug
before render and see@message.errors
. Also you can addajax:complete
to debug in browser console. – Utile