escargot agile
escargot agile

Reputation: 22379

Ajax in rails for a nested resource

I have these pahts in my website:

/stops/13090
/stops/13090/services

so in config.rb I have:

resources :stops do
  resources :services, only: [:index]
end

When the user navigates to /stops/13090, I would like to show a webpage that includes both the data for /stops/13090 rendered by the StopsController and the data for /stops/13090/services rendered by the ServicesController. So I'd like it to initiate two HTTP requests: one to /stops/13090 and one to /stops/13090/services, and combine both in the web page.

I've tried googling various terms, but I'm not sure what exactly I should search for to solve this. I've tried using render partial: but it seems unrelated to ajax.

I prefer HAML, but it's perfectly fine if the answers are for a different template engine.

Upvotes: 0

Views: 272

Answers (1)

Prasad Surase
Prasad Surase

Reputation: 6574

You can send a ajax request when a particular element in the page has been loaded.

$( "#result" ).load(some_url_path, function() {
  return $.ajax({
    url: "/stop/" + stop_id + "services"
  });
});

and in the services.js.haml, you can load the services collection on the 'stop' show page. checkout https://api.jquery.com/load/

Upvotes: 1

Related Questions