1

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 ?

3
  • 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. Commented 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... @ShabiniRajadas Commented Jul 10, 2017 at 6:47
  • 1
    When 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. Commented Jul 10, 2017 at 6:52

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.