konnigun
konnigun

Reputation: 1805

Proper way to perform AJAX call in Rails

I am currently performing AJAX in "oldschool php way":

/home/index.html

  <%= button_to 'Get message', :id => 'get_message' %>
  <div id="message"></div>

assets/javascript/index.coffee

$(document).ready ->
  $('#get_message').on(
    'click',
    ->
      $.post(
        '/home/message/',
        (data) ->
          $('#message').text(data.message)
      )
  )

app/controllers/home

def message
  respond_to do |format|
    format.json { render :json => { :message => 'Hello, world!' } }
  end
end

Is the way I do it a good practice or is there any better way? Thanks!

Upvotes: 0

Views: 64

Answers (1)

fotanus
fotanus

Reputation: 20116

To stuff like that, you could use :remote option in button_to, form_for and link_to tags.

Upvotes: 1

Related Questions