I'm trying to pass a variable into a method. Here is an example:
<script>
var b = 1
var c = "string" + "<%= method.a(b).to_s%>"
</script>
which doesn't work.
This works:
<% @b = 1%>
<script>
var c = "string" + "<%= method.a(@b).to_s%>"
</script>
Any ideas would be appreciated.
I'm trying to pass a variable into a method. Here is an example:
<script>
var b = 1
var c = "string" + "<%= method.a(b).to_s%>"
</script>
which doesn't work.
This works:
<% @b = 1%>
<script>
var c = "string" + "<%= method.a(@b).to_s%>"
</script>
Any ideas would be appreciated.
Share Improve this question edited Sep 22, 2015 at 20:24 the Tin Man 161k44 gold badges221 silver badges306 bronze badges asked Sep 22, 2015 at 19:04 RichardlonesteenRichardlonesteen 5945 silver badges19 bronze badges 2- possible duplicate: stackoverflow./questions/4959770/… – K M Rakibul Islam Commented Sep 22, 2015 at 19:08
- I don't want to close as a duplicate of that question just for the 'accepted' answer :} Maybe another? stackoverflow./questions/14080154/… , stackoverflow./questions/5655096/… – user2864740 Commented Sep 22, 2015 at 19:09
4 Answers
Reset to default 1You can't evaluate a JavaScript variable inside your Ruby method. Also, your current script has to be outside the asset pipeline, which is usually a bad practice.
One simple solution is to pass the variable as a data attribute. You would run your method, then pass the result with jQuery after the DOM has loaded. This allows you to keep JavaScript in the asset pipeline and pass it data evaluated by Ruby server-side.
Note: @b
should be defined on your controller:
<div id="foo" data-bar="<%= method.a(@b).to_s %>"></div>
<script>
$(document).ready(function() {
var b = $(#foo).data('bar');
var c = "string" + b;
});
</script>
The other option is to render your script asynchronously with unobtrusive JavaScript in Rails.
There is a nice gem gon for performing this job. Using this gem you can do this:
Inside controller:
gon.push(variable_name: 1)
Inside your js files you can retrieve this variable:
gon.variable_name
It's working for :js or :coffee files.
Ruby code is executed at the server while Javascript is executed by the client's browser.
Ruby code marked by <%= %>
is executed at the server and then delivered to the client, where Javascript code will be executed. That is why the first snippet does not work while the second one does: in the second snippet, <% @b = 1 %>
gets evaluated before sending out HTML to the requester.
With that said, there is a way to do what you want: use Ajax to pass the Javascript variable to the server, execute whatever you need and send back an answer to the client.
You need to describe your process flow better.
Case 1: The value of the variable is determined by user action. Then make an ajax call to rails object method to get the puted value to js.
Case 2: The value of the variable is determined by the app(server side). Then you can do:
<% @b = 1%>
<script>
var b = <%= @b %>;
var c = "string" + "<%= method.a(@b).to_s%>";
</script>
Same value will be available to JS and server method. But this is not a clean approach. View should not set the value.
And also, keep js in asset pipeline as suggested by @jeffD23
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742300050a4417864.html
评论列表(0条)