J
J
jetus2014-06-05 17:10:27
JavaScript
jetus, 2014-06-05 17:10:27

How to send a message without reloading the page (Ruby on Rails)?

The page has a div with CSS property display: none; When you click on it, here, a block with two fields for entering values ​​\u200b\u200bis shown (Gender and Age). If you click "Submit data", then the data simply goes away, leaving behind the same div block with the property display: block;
How can I make it so that when a message is sent, the block closes and a message about successful sending is displayed in its place? No page reload.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dimitriy, 2014-06-05
@jetus

In a simplified form, this is how it is.
In the view:

<div class="msg"></div>
<div class="form-wrap" style="display:block">
<%= form_tag('/feedback', remote: true, format: :json, id: 'feedback') do %>
....

in controller
def send_email
....
  respond_to do |format|
    # if success
    msg = success ? 'You request will be success processed'
                  : 'Ooops. Error occurred. Try later.'
    format.json { render json: {msg: msg}.to_json }
  end
end

CoffeeScript:
$('#feedback')
  .on 'ajax:before', ()->
    $('.form-wrap').hide()
    $('.msg').html('Sending...')
  .on 'ajax:success', (e, data)->
    $('.msg').html(data.msg)

More details can be found here

A
animhotep, 2014-06-05
@animhotep

xmlhttprequest.ru for example

F
Finist, 2014-06-05
@Finist

Your main task is to submit the form with ajax to the server, hide the unnecessary block here is the easiest
stackoverflow.com/questions/6723334/submit-form-in...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question