0

Passing data from Ruby to Javascript is easy, for example:

<script type="text/javascript">

    function change_value(val){

      alert ("<%= @alert %>")   
      }
    }
</script>

This will sent an alert with the data stored at the alert variable in the controller.

But I don't know how it works in the opposite direction, for example, if I need to store an element id into a controllers variable:

<script type="text/javascript">

    function change_value(element){
      <% @element_id = *** element.id *** %>

      alert ("<%= @element_id %>")   
      }
    }
</script>

The real deal is up next, the code surrended by *** are supposed to be the needed javascript values (@billeable_qts is a Hash):

<script type="text/javascript">

  function change_value(product){

    <% @billeable_qts[****product.id****] = ***document.getElementById('product.id').value****  %>
    <% @billeable_qts.each do |key, value| %>
       <% alert = "Key = " + key + ", value: " + value.to_s%>
       alert ("product: <%= alert %>")
    <% end %>


  }

</script>
4
  • 1
    what are you trying to do with the variable in your controller? If you just want to alert the value, you can just have plain javascript and it's all client side function change_value(element) { alert(element.id) } If you want to do something with it in the controller, e.g. add something to the database, send an email. You will have to send a request to that controller action, e.g. function change_value(element) { doPostRequestTo('controller/change_value?value=' + element.id) } Commented Jan 18, 2018 at 21:32
  • But I dont know how it works in the opposite direction, js can send a request to your rails server that contains any information you want. The topic is called ajax. How can I access the id attribute of the javascript "element" -- With js code, you can retrieve any element on the page using the element's id. For instance, if you want to send back the value of a textfield after the user clicks on a button, then your js code can attach on onclick hander function to the button, which uses (the inserted by rails) @element_id to retrieve the textfield's value. Commented Jan 18, 2018 at 22:57
  • Rails provides the means to write ajax code in your views, but I find it so overly complex that I just write the js by hand myself. The bottom line is that to be a web developer, you have to know a lot of different languages: html, js and jQuery, and a server side language like ruby along with rails. at a minimum. Commented Jan 18, 2018 at 23:08
  • I need this because Im trying to fill a hash with the element ids and a user value only when the user add it, there can be many values entered at a time and I need to interact with those values and identifiers. Thanks!! Commented Jan 19, 2018 at 15:16

1 Answer 1

2

I'm assuming that you are referring to an embedded Ruby file with something like a js.erb extension.

Those instance variables, such as @element_id, are used to generate a javascript file to then run client side (ie in the browser)

It's important to point out here, that the variable @element_id is only used to generate the javascript file then you can essentially consider it "gone" after that javascript file is created.

That is why you can "pass" Ruby variable values into the javascript file. However, you cannot pass values obtained by the javascript when run client side back to those Ruby variables because they essentially no longer exist as their only purpose was to create the javascript file.

So the controller action used to generate that file has essentially served it's purpose, and it and it's variables, etc are no longer available.

Now, it appears that you have a javascript variable, obtained client side, that you want to manipulate before posting it in the js alert.

You have two choices

1) You can just manipulate it using pure javascript.

<script type="text/javascript">

    function change_value(element){
      idToAlert = element.id;
      idToAlert++;
      alert (idToAlert);   
      }
    }
</script>

2) If for some reason that value needs to be modified based on something like database values that the Rails app has access to, you can post it to a controller and action then return it back to the javascript. (Note: I'm using jQuery library here for efficiency)

<script type="text/javascript">

function change_value(element){
$.ajax({
  url: "/controller/update_element_id",
  type: "POST",
  data: {"old_element_id": <%= @element_id %>, "new_element_id": element.id},
  success: function(response) {
    alert (response);
  },
  error: function(response) {
    console.log('error: ' + response);
  }
});
}
</script>

Controller#update_element_id might be an endpoint for a JS POST request that looks like this.

def update_element_id
  item = Model.where(element_id: params["old_element_id"]).first
  item.update_attributes(element_id: params["new_element_id"])
  render json: {element_id: item.element_id}
end

I hope this helps. Glad to provide any additional clarification.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.