Reputation: 189
In rails you can do this:
<% name = @user.name %>
<script>
var name = <%= name%>;
alert(name);
//prints the name of the user
</script>
You can do this in reverse, like this
<% name = @user.name %>
<script>
var name = "John";
<% name %> = name;
</script>
<% @user.name = name %>
<%= name %>
<!-- prints the name of the user, in this case "John" -->
Obviously this doesn't work, but for the purpose of an example
The point is, can you pass the value of a javascript variable to a ruby variable in rails
Upvotes: 4
Views: 3343
Reputation: 54902
No, you can't pass a variable directly from javascript to rails:
The only way to communicate client -> server is to use a request. You can use AJAX (Asynchronous Javascript And XML) to send a request from the client to the server without reloading the page.
You have to understand some concepts about code on the server vs client side. The process of "displaying a page from a website":
http://myapp.com/users
Upvotes: 5