I am trying to call a ruby method from erb file. This method takes one parameter. This parameter value is entered by user in a form. How do I send this as argument to the method?
something like this works. i.e when I send string instead of variable
<% construct('Text')%>
But how to send variables? i.e I need something like this
<% construct(Variable)%>
My variable is javascript variable
var Variable = Search.val();
Should I create a ruby variable before passing? Any example code for this will be really helpful
I feel like there’s a fundamental flaw in how you think ruby works…
Ruby is a server-side language, and javascript is a client-side language. The ruby code in a Ruby on Rails application sends the client (A user’s browser) the HTML, Images, CSS, and other assets it needs for a user’s browser to display what they need to see. Javascript is only viewed by the browser, and Ruby code is only viewed by your server (Which the browser has no knowledge of).
You cannot use a javascript variable to send to a Ruby method. You either need to re-implement your helper method in javascript to get the functionality you need, or you need to perform an Ajax request to get the javascript to make a request to your server, which your Ruby on Rails controller can then handle and give the appropriate response to.