1

I receive a google ID from an Ajax call and then want to use that ID to update a button on my view.

Here is the code in new.js.erb, which is linked to a new.html.erb.

Problem is, I don't know how to pass the variable's content. Restaurant is a json. The alert returns the correct ID and when I search my db on the terminal with the returned google id I find the restaurant.

Here is the code:

alert(restaurant["google_id"]);
    var google_id = restaurant["google_id"];
    $("#rating_bar").html("<%= escape_javascript(render 'reviews/buttons/full_profile_rate_restaurant', :google_id => "+google_id+".html_safe) %>");

What happens is that the variable being passed is the string "google_id" instead of the combination of letters and numbers that is the google ID. I've tried multiple approaches, this is just one of many wrong one - I think this question is pretty easy for anyone who knows their JS really well.

6
  • Is your alert giving you the correct response? Is restaurant a hash? An object? - Please explain how restaurant is being populated with data. Commented Mar 23, 2013 at 19:06
  • Is the JSON data decoded into an object already? Or is it still a string containing JSON data? If its still a string, first make sure you parse it: var parsed=jQuery.parseJSON(restaurant); Commented Mar 23, 2013 at 19:17
  • Commander, all I need from the object is that one variable: the google ID, which is a string. I don't need the rest of the object. I do get the Google ID but the problem is how to pass it to my partial. Right now, every attempt I make passes the literal string google_id instead of the variable content, which is the string aw4t3q09wt4heoitp (for example) Commented Mar 23, 2013 at 19:27
  • Sorry, i missed your newer text in your latest edit. If the ID is correct at that point, then please try to pass it to #rating_bar alone and see if the displays result is correct. $('#rating_bar').html(google_id); Commented Mar 23, 2013 at 19:30
  • Yes Commander it passes. Looks like the problem is integrating the variable in a escapE_javascript call? Commented Mar 23, 2013 at 19:35

1 Answer 1

3

It is not possible to pass a JS variable to the ruby partial.

As Ryan Bigg explained for the same type of problem here, its not possible to send the variable while rendering that partial. We need to work out some thing else. Even i also had the same issue once.

Alternatively,

if that is google_id is only a variable to display in the partial, then update those divs manually after rendering that partial.

like

$("#rating_bar").html("<%= escape_javascript(render 'reviews/buttons/full_profile_rate_restaurant', :google_id => "sample_id") %>");
// Now update the required elements
$("#what-ever-ids").text(google_id);

or just create some other action in that controller, and call send an ajax request to that action, and there you will have this js variable, and in that js.erb file render the same partial which you actually want to update with the google_id variable.

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

1 Comment

Hey Saggar you are correct and I realied I make the same mistake over and over again. I have to find a way to re-render the partial after the model object has been created, basically, and I am racking my brains on how to do this.

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.