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!
var numWords = lines.lengthwhere do you setlines'? (if it's a text)