Ajax call for controller action on rails

I am trying to make an ajax call for my controller

class PatientRecordController < ApplicationController def export .... end end 

In my javascript file I have

 $(document).ready(function(){ freezeTopRow($('#dataTable')); $("#export").click(function(){ $.ajax({url: "patient_record/export", type: "POST"}); }); }); 

when I check the item and debug, and when I click the export tag on my page. I click on a function but never gets to the controller

Also I have 2 controllers and 2 views. In my other controller and view, I am doing the same thing and it works

+6
source share
2 answers

You checked that you have routes.rb something like:

 post 'patient_record/export' 

Perhaps Rails does not know the route, so ajax does not work (if you can go to the action from your browser, it means that you only have a GET set, you can check that the request type change in the ajax call)

+8
source

You also need a route for the export action in your config/routes.rb , something like

 resources :patient_records do member do post :export end end 

You can check if this exists by running rake routes | grep 'export' rake routes | grep 'export' .

+2
source

Source: https://habr.com/ru/post/948224/


All Articles