1

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

2
  • Your javascript variable and your ruby code are not in the same world. You need to create a portal to allow one to communicate with the other. Commented Mar 15, 2013 at 0:08
  • The Ruby code in your code sample is only executed when generating the Javascript to be sent to the browser. You can't call Ruby code directly from Javascript that way, you'll have to use AJAX: guides.rubyonrails.org/ajax_on_rails.html Commented Mar 15, 2013 at 0:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer, i didn't know what to do. i will try what you say and hope it works. thank you so much
My pleasure! Welcome to Rails =)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.