简体   繁体   English

在rails中对控制器动作进行ajax调用

[英]making an ajax call to controller action in rails

I am trying to make an ajax call to my controller 我正在尝试对我的控制器进行ajax调用

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

In my javascript file i have 在我的javascript文件中我有

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

when i inspect element and debug and when i click the export tag on my page. 当我检查元素和调试,当我点击我的页面上的导出标签时。 i hit the function however it never gets to the controller 我点击了该功能,但它永远不会到达控制器

Also I have 2 controllers and 2 views. 我还有2个控制器和2个视图。 In my other controller and view I do the same thing and it works 在我的另一个控制器和视图中,我做同样的事情,它的工作原理

Have you checked that you routes.rb have something like: 你有没有检查过你的routes.rb有类似的东西:

post 'patient_record/export'

Maybe Rails doesn't know the route so the ajax isn't working (if you can get to the action from your browser it means that you have only a GET set, you can check that changing the type of the request in the ajax call) 也许Rails不知道路由所以ajax不工作(如果你可以从你的浏览器到达操作,这意味着你只有一个GET设置,你可以检查在ajax调用中更改请求的类型)

You also need a route for the export action in your config/routes.rb file, something like 您还需要在config/routes.rb文件中进行export操作的路由,例如

resources :patient_records do
  member do
    post :export
  end
end

You can check to see if this already exists by running rake routes | grep 'export' 您可以通过运行rake routes | grep 'export'来检查是否已存在 rake routes | grep 'export' . rake routes | grep 'export'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM