V
V
vadimstroganov2016-02-01 18:54:52
Ruby on Rails
vadimstroganov, 2016-02-01 18:54:52

Why does this alert pop up after submitting the form?

Good afternoon.
There is a form:

= form_for :callback_profile_params,
                 :url    => { :controller => 'users', :action => 'callback_profile' },
                 :method => 'POST',
                 :remote => true,
                 html: { id: 'callback_profile', :class => 'send-form' } do |f|
        = hidden_field_tag :authenticity_token, form_authenticity_token
        = f.text_field :user_id, :type => 'hidden', :value => current_user.id
        .form-group
          = f.text_field :title, :placeholder => 'Тема обращения'
        .form-group
          = f.text_area :message, :placeholder => 'Вопрос'
        = submit_tag 'Отправить', :class => 'btn btn-primary', :id => 'btn_submit'

Coffee:
$ ->
  $("#callback_profile").on("ajax:success", (e, data, status, xhr) ->
    $("#btn_submit").replaceWith data
   ).on "ajax:error", (xhr, status, error) ->
    $("#btn_submit").replaceWith data

In controller:
before_action :authenticate_user!

  def callback_profile
    render :text => 'Запрос успешно прошел.'
  end

On pressing the send button, an alert immediately pops up with the text: "Message was not sent. Client error or Internet connection problems."
If you look at the console, then the ajax request went through and returned the code 200.
I don’t understand where this alert comes from, and somehow Coffee doesn’t reach the execution because of it ..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeniy Trebin, 2016-02-02
@evgeniy_trebin

Firstly, in the 'ajax:error' error handler, the data variable is not set
Secondly, try to do this

def callback_profile
    render json: { text: 'Запрос успешно прошел.'}
end

$ ->
  $("#callback_profile").on("ajax:success", (e, data, status, xhr) ->
    $("#btn_submit").replaceWith data['text']
   ).on "ajax:error", (xhr, status, error) ->
    $("#btn_submit").replaceWith 'Error!'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question