简体   繁体   English

将 javascript 选择变量传递给 rails 控制器

[英]Pass javascript selection variable to rails controller

I'm working on a rails (v.5.2.3) app that is supposed to provide data filter abilities for spatial data (model "measurements") based on (1) different variables and (2) a lasso selection on a map.我正在开发一个 Rails (v.5.2.3) 应用程序,该应用程序应该为基于 (1) 不同变量和 (2) 地图上的套索选择的空间数据(模型“测量”)提供数据过滤能力。 Both of these filter options should be available in the same view ("welcome/index").这两个过滤器选项都应该在同一视图(“欢迎/索引”)中可用。

The relevant part of the controller for preparing and filtering the data:用于准备和过滤数据的控制器的相关部分:

# app/controllers/welcome_controller.rb
def index

    @selected_measurements = Measurement.where(
        "measurements_id IN (?) OR name = ?",
        params[:spatial_lasso_selection],      
        params[:query_site_name]
    ).all

    gon.selected_measurements = @selected_measurements.to_json
end 

In the view I use form_tag for the filter variable definition (1):在视图中,我使用 form_tag 作为过滤器变量定义 (1):

# app/views/welcome/index.html.erb
<%= form_tag({controller: "welcome", action: "index"}, method: "get") do %>
    <%= label_tag(:query_site_name, "Site name:") %>
    <%= text_field_tag(:query_site_name, (params[:query_site_name] or "")) %>
    <%= submit_tag("Search") %>
<% end %>

In the assets directory I have a javascript file that constructs a leaflet map (via leaflet-rails), adds the point data as markers and enables the leaflet-lasso plugin for (2) lasso selection on the map.在资产目录中,我有一个 javascript 文件,它构建传单地图(通过传单导轨),将点数据添加为标记,并启用传单套索插件以在地图上进行 (2) 套索选择。 This code is embedded in the welcome view to display the map there.此代码嵌入在欢迎视图中以在那里显示地图。

The lasso selection gives me a javascript array of measurement_ids.套索选择给了我一个measurement_ids的javascript数组。 I want to send this array to my rails controller to filter the data and update the view ( params[:spatial_lasso_selection] ).我想将此数组发送到我的 rails 控制器以过滤数据并更新视图( params[:spatial_lasso_selection] )。 I learned that I should do this with an ajax request, but I have difficulties implementing it.我了解到我应该使用 ajax 请求来执行此操作,但是我在实现它时遇到了困难。 I don't understand how form_tag and raw javascript can be used together to make both (1) and (2) work seamlessly.我不明白如何将 form_tag 和原始 javascript 一起使用来使 (1) 和 (2) 无缝工作。

I figure the ajax call has to look like this:我认为 ajax 调用必须如下所示:

$.ajax({
    type: "get",
    url: '/welcome/index',
    data: { spatial_lasso_selection: JSON.stringify(lasso_selected_measurements) },
});

I have problem also with ajax request.我也有 ajax 请求的问题。 Instead I use input text value property to pass my javascript variable to the controller.相反,我使用输入文本值属性将我的 javascript 变量传递给控制器​​。 Firstly just make simple form with hidden input property that have specific id in your index.html .首先,只需使用在index.html中具有特定 id 的隐藏输入属性制作简单的表单。 Next create button that will send the variable to下一个创建按钮,将变量发送到

<form id "myForm" action="/welcome/index">
  <input type="hidden" id="myVar" name="varParams">
</form>
<button id="btnSend">SEND</button>

Then, create javascript function to set the value of the input text with our required variable when the button is clicked.然后,创建 javascript 函数以在单击按钮时使用我们所需的变量设置输入文本的值。 Just put below code in your index.html .只需将下面的代码放在您的index.html

<script>
document.getElementById('btnSend').addEventListener('click', function () {
  document.getElementById("myVar").value = "myValue";
  document.getElementById("myForm").submit();
});
</script>

Next, just retrieve the myVar params in your welcome_controller.rb .接下来,只需在您的welcome_controller.rb检索myVar 参数。

  def index
    myVar = params[:varParams]
    # Do something with myVar variable  
  end

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

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