V
V
vadimstroganov2015-08-13 16:30:17
JavaScript
vadimstroganov, 2015-08-13 16:30:17

How to send data via AJAX in Rails?

Hello!

There is a page with products, each product has a "Details" link, and a data-id containing the product id is attached to each link.

I have an item_search_by_id(id) method that takes an item id and searches for all item information by id.

I don’t understand how I can pass data-id to the method and get a response. Tell me please

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
TyzhSysAdmin, 2015-08-13
@POS_troi

rusrails.ru/working-with-javascript-in-rails

T
thepry, 2015-08-13
@thepry

You must have a route created to receive data by ide, there must be a method in the controller, and then just an ajax request (coffeescript code):

item_id = $(element).data('id')
$.ajax
      url: 
      type: 'get'
      data: { 'item_id': item_id }
      dataType: 'json'
      success: (response) ->
         ...
      error: (response) ->
        ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question