如何在ruby中使用javascript变量 [英] How to use javascript variables in ruby

查看:79
本文介绍了如何在ruby中使用javascript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是rails的新手,我想使用javascript变量进入ruby嵌入式代码在数据库中进行查询,

I'm new in rails and I want to use a javascript variable into ruby embedded code to make a query in the database,

这是我的代码

function modify(){
    var select = document.getElementById("special_id").value;
    var select2 = document.getElementById("target_area_id");
    select2.options.length = 1;

    <% @values = TargetArea.where(:special_id => select) %>

    select2.options[select2.options.length] = new Option(<%= @values.count %>,"1");
}

我想将我的select中获得的值与字段的id进行比较。

I want to compare the value obtained from my select with the id of a field.

请告诉我如何在Ruby中执行此操作?谢谢

Could you please tell me how can I do this in Ruby? Thanks

推荐答案

一种方法可能是我所知道的最简单的方法,也是传统的处理方法,执行Ajax请求。 JavaScript是客户端的,Ruby通常是服务器端的。

One method that is probably the easiest that I know of, and a conventional way to go about handling this, is to perform an Ajax request. JavaScript is client-side and Ruby is server-side usually.

您可以在路径文件中创建一个路由,然后在相应的控制器中创建一个动作使用JavaScript而不是HTML进行响应。在该JavaScript文件中,您可以更改或修改页面上的内容,并可以访问您更改的变量,重新呈现页面的某些部分,等等。我会在这里使用jQuery,因为它有点清洁。

You can create a route in your routes file, and then, in the corresponding controller, create the action which responds with the JavaScript instead of HTML. In that JavaScript file, you can change or modify things on the page and have access to the variable you changed, re-render parts of the page, and so on. I'll use jQuery here, because it's a bit cleaner.

在你的路线文件中:

post 'some_controller/make_a_change', to: 'some_controller#make_a_change'

然后,在上面定义的函数中:

Then, in the function you have defined above:

$.ajax({
   url:'/some_controller/make_a_change', //Defined in your routes file
   data:(
     'special_id=' + $('#special_id').val() + '&' +
     'target_area_id=' + $('#target_area_id').val()
   )
})

在SomeController.rb中,您可以通过 params 访问您的JavaScript值:

In SomeController.rb you can access your JavaScript values via params:

def make_a_change
   some_special_id = params[:special_id]
   taid = params[:target_area_id]
   @values = TargetArea.where(:special_id => some_special_id)

   respond_to do |format|
      format.js { render 'make_a_change' } #make_a_change.js.erb
   end
end

然后,我认为你可以在make_a_change.js.erb中完成剩下的工作,每当你调用其他函数并且你的Ajax请求成功时它就会运行。

Then, I think you can just do the rest in your make_a_change.js.erb, which will run each time you call that other function and your Ajax request is successful.

var select2 = document.getElementById("target_area_id");
select2.options.length = 1;
select2.options[select2.options.length] = new Option(<%= @values.count %>,"1");

根据您的需要调整。希望这会有所帮助!

Tweak that to your needs. Hope this helps!

更新:

我刚刚意识到这一行:

select2.options[select2.options.length] = new Option(<%= @values.count %>,"1");

可能无效。如果它是一个HTML选择框或类似的东西,您可以将它放在自己的视图中,并在适当的位置在make_a_change.js.erb中重新渲染它。

might not work. If it's an HTML select box or something similar, you could put it in its own view, and re-render it in make_a_change.js.erb in the proper spot.

像这样:

     $('#some_div').html('<%= j render 'select2_list', values:@values %>')

大多数情况下,我的答案背后的主题是:如果你想要与JavaScript函数中的Ruby代码或服务器数据进行交互,这是一个函数,当您单击按钮或选中某个框或其他内容时调用该函数时,您必须向服务器发送Ajax请求并在您的服务器中处理请求控制器。这将使您更接近Rails,并进一步尝试使用 = 符号链接两个不同的语言变量。

Mostly, the theme behind my answer is this: If you want to interact with your Ruby code or server data from a JavaScript function, which is some function called when you click a button or check a box or something along those lines, you will have to send an Ajax request to your server and handle the request in your controllers. That will then bring you closer to Rails and further from trying to link two different language variables with an = sign.

这篇关于如何在ruby中使用javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆