Is it possible to use JS code with in ruby code?

I am created Rail application with erb views. I want to use some JS code with in ruby code like following

<script>
   $('#select-service').click(function() { 
      var service='<%= Service.find("this.id") %>';
    });
</script>

I want to query based on user selected value. How to replace "this.id" used above and how to insert js code instead of "this.id" ?

Upvotes: 0

Views: 451

Answers (1)

Yana Agun Siswanto
Yana Agun Siswanto

Reputation: 2032

You cant. this is only available in the browser.

One of the option you can do is to do it via ajax.

for example.

<script>
   $('#select-service').click(function() { 
      $.get('/service/' + this.id, function(result){
           service = result;
     }
    });
</script>

Upvotes: 1

Related Questions