简体   繁体   中英

How to use javascript variables in 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,

This is my code

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");
}

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

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

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.

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.

In your routes file:

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()
   )
})

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

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!


UPDATE:

I just realized this line:

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

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.

Like so:

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

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.

Since Ruby is a Server based language like php or asp/asp.net, using code from the server in the page is very easy like printing it, so if you want to change a javascript variable using server side values is easy. However doing the opposite is a different thing. I would send those values to the server using a proxy call (jQuery most likely). The solution you are asking for calls for the use of a javascript framework like jQuery, not rails that I know.

Try using a jQuery ajax proxy and refreshing data in the client based on server´s response, that will do the trick.

check http://www.jquery.com , use rails to create services that will respond with JSon to your queries.

I hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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