I have js variable id
<a id="ajax" auction=<%= @product.id %> href="#" class="product active"><%= @product.name %></a>
<script>
var id = $(this).attr('auction');
</script>
I want to assign the id value to ruby variable
<% @v = 'JS VARIABLE' %>
Is it possible? How to Assign ?
-
You cannot assign a javascript variable in rails object. May I know for what you wanted to do that? In that case you can check other js methods to achieve that.Shabini Rajadas– Shabini Rajadas2017年07月10日 06:35:59 +00:00Commented Jul 10, 2017 at 6:35
-
I am displaying 10 products names, when we click first product name, displaying that product details using ajax call.. so i have to give edit option to that product... @ShabiniRajadaskhalidh– khalidh2017年07月10日 06:47:55 +00:00Commented Jul 10, 2017 at 6:47
-
1When you are getting product list via ajax, on that place you should add product edit url as href block with DOM element of product list on ajax on_success callback.Prakash Laxkar– Prakash Laxkar2017年07月10日 06:52:34 +00:00Commented Jul 10, 2017 at 6:52
1 Answer 1
I'm not sure what is the final result you're trying to achieve, but at the point that your script runs, the server has already finished its rendering, and so it is impossible to assign it to a ruby variable in that way.
You can send an ajax request to the server (maybe after some user input), and update only a small part of the page if that is what you want?
There is an old but still useful Railscast on this: http://railscasts.com/episodes/205-unobtrusive-javascript?view=asciicast
Comments
Explore related questions
See similar questions with these tags.