0

I have been looking everywhere and tried every suggestion I could find for this but it is still not working. Basically I am trying to take whatever is entered, split it into separate words, and then make each word a link. I trying to take a value from the database and passing it to javascript so that I can have a variable in javascript be that value. Here is the code:

in the controller:

def show
  @icelandic_reader = IcelandicReader.find(params[:id])
  @icelandic_reader_json = @icelandic_reader.text.to_json;

respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @icelandic_reader }
 end
end

In the .js.erb file:

window.onload = function() {
  var myText = <%= @icelandic_reader_json %>;
  console.log("test1");
  var words = myText.split(" ");
  console.log("test2");
  var numWords = words.length;
  var textToInsert;

  for (var i = 0; i < numWords; i++) {
      console.log("inside for loop");
      textToInsert += "<a href=#>" + words[i] + "</a>" + " ";
  }

  document.getElementById('icelandic_reader_text').innerHTML = textToInsert;

}

I don't get any errors, but I am not seeing any of my test console logs appear. I have tried every way that I have found how to assign the ruby variable but it just won't work. I have ruby 3.2.6. Thank you!

7
  • In var numWords = lines.length where do you set lines Commented Dec 3, 2012 at 3:29
  • Thanks, I fixed that, I renamed a few things before posting and missed that. Still, it doesn't fix the problem. I don't even think it is getting passed the first line since I never see "test1" in my console. Commented Dec 3, 2012 at 3:36
  • 1
    You must surround the text with '? (if it's a text) Commented Dec 3, 2012 at 4:00
  • If I do that, it runs but all that gets displayed is "undefined" Commented Dec 3, 2012 at 4:17
  • Check out the GON gem (github.com/gazay/gon). I think you'll find it a much easier tool for passing ruby data to javascript. Commented Dec 3, 2012 at 4:56

2 Answers 2

2

This looks strange to me. If @icelandic_reader.text is just a string, why are you calling to_json on it? And if it's a property of @icelandic_reader, which you have access to from the view, then why are you even assigning it as an instance variable? And why do you even need the render json call? Rails will just look for show.js.erb in your views folder.

Try this:

# controller

def show
  @icelandic_reader = IcelandicReader.find(params[:id])

  respond_to do |format|
    format.html # show.html.erb
    format.json 
  end
end

And in your js.erb:

# js.erb

var myText = "<%= @icelandic_reader.text %>";
Sign up to request clarification or add additional context in comments.

Comments

1

I just want to close this question, that is why I am answering my own question. Teeg told me to use Gon, which is what ended up working for me but he put in in a comment so I couldn't vote it as the answer.

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.