简体   繁体   English

AJAX请求无法从Rails应用程序中的控制器获取实例变量

[英]AJAX request can't get instance variable from controller in Rails application

I'm trying to get data from my controller into a javascript file in a rails application. 我正在尝试将数据从控制器获取到Rails应用程序中的javascript文件中。 The html immediately invokes an ajax request using the select option and the organization id as parameters. html使用select选项和组织ID作为参数立即调用ajax请求。

In a before_filter I have 在一个before_filter我有

def set_org_id
    if params[:id].present?
        @org_id = klass.find(params[:id]).id
    else
        @org_id = 0
    end
end

And in the js file I have: 在js文件中,我有:

$.ajax({ url: "/admin/analytics/filter",
        type: 'GET',
        data: {
          time_frame: selectedId,
          organization_id: <%= @org_id %>
        }
})

If I hard code a number as the organization_id everything works fine, but when I try to pass the data from the before filter, I end up with no id in the page. 如果我将数字硬编码为organization_id,则一切正常,但是当我尝试传递来自before过滤器的数据时,最终页面中没有id。

I should add that the index route is admin/analytics/. 我应该补充一点,索引路由是admin / analytics /。 The filter action is admin/analytics/filter. 筛选器操作是admin / analytics / filter。 The user never navigates to that page, only ajax hits the filter route to get relevant data. 用户永远不会导航到该页面,只有ajax会点击过滤器路线以获取相关数据。 Could this be that the ajax request is being send before the instance variable is set? 可能是在设置实例变量之前发送了ajax请求吗? If so, what is the proper solution? 如果是这样,什么是正确的解决方案?

Your JS won't be able to access your @instance variables unless you call them from your controller itself. 除非您从控制器本身调用@instance variables 否则您的JS将无法访问它们。 The problem here is that if you're loading the ajax to access an instance varialbe - which simply won't work. 这里的问题是,如果您要加载ajax来访问实例varialbe-这根本行不通。

Let me explain... 让我解释...


JS JS

Javascript is known as a client side language - meaning it provides you with the ability to access elements in your HTML / DOM with relative impunity. Javascript被称为客户端语言,这意味着您可以相对不受惩罚地访问HTML / DOM中的元素。 The problem here is that Rails / Ruby, much like PHP, is server-side , and consequently is only able to provide rendered data to your browser 这里的问题是,Rails / Ruby(类似于PHP)是服务器端的 ,因此只能向浏览器提供渲染的数据。

This means that calling the following simply won't work: 这意味着调用以下命令根本行不通:

data: {
   time_frame: selectedId,
   organization_id: <%= @org_id %>
}

As explained, the reason for this is that you cannot access the @org_id from your Javascript. 如前所述,其原因是您无法从Javascript访问@org_id Firstly, you don't know if the @org_id variable will be set (in case you want to use the Rails helpers), and secondly, your JS won't be able to access the variable anyway (because it's Rails) 首先,您不知道是否会设置@org_id变量(以防您想使用Rails帮助器),其次,您的JS仍然无法访问该变量(因为它是Rails)

-- -

Fix 固定

The purest fix for this is to somehow create the data you need in the DOM. 最纯粹的解决办法是以某种方式在DOM中创建所需的数据。 I see from your answer that you have set a hidden field for your form. 从您的答案中可以看出,您已经为表格设置了一个隐藏字段。 A much better way to do this is to set an HTML5 "data" attribute , or to use an id 更好的方法是设置HTML5“数据”属性 使用id

You'd be better doing this: 您最好这样做:

<%= form_tag route_path, data: { org_id: @org_id } %>

This will give you the ability to call: 这将使您能够调用:

$(document).on("submit", "form", function(){
   $.ajax({
      ...
      data: {
        time_frame: selectedId,
        organization_id: $(this).data("org_id")
      }
   });
});

I was able to solve this by passing the data to a hidden field in the html.erb page. 我能够通过将数据传递到html.erb页面中的隐藏字段来解决此问题。

<%= hidden_field_tag('org_id', @org_id) %>

Then in the javascript refer to the data through the selector. 然后在javascript中通过选择器引用数据。

organization_id: $('#org_id').val()

I don't love this, but at least it works. 我不喜欢这个,但至少可以奏效。

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

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