chopper draw lion4
chopper draw lion4

Reputation: 13487

Rendering ERB in models: "no implicit conversion of ERB into String"

I am rendering an ASCII map that looks like the following:ASCII map, is it not so pretty?.

I want the asterisk in the map, which represents a character, to be red. Right now this characters asterisk is being assigned to its location as follows:

  def mark_character_coordinates
    WORLD.terrain[@character.y_coordinate][@character.x_coordinate][0][0] = "*"
  end

I want my characters location on the map (the asterisk) to render as red. My idea is to try to wrap the asterisk in a span and give it an id. Then go into my CSS and make ID color red. My problem is I am not sure how to insert ERB into the model and have it render as such in the view. After reading similar problems on stackoverflow, this is the closest I got:

  def mark_character_coordinates
    WORLD.terrain[@character.y_coordinate][@character.x_coordinate][0][0]
      = ERB.new("<span>*</span>")
  end

What should I do? How do I insert ERB into a variable in the model and have it render accordingly in the view?

Upvotes: 0

Views: 839

Answers (2)

Ege Ersoz
Ege Ersoz

Reputation: 6571

The best and easiest way to do this is to use JQuery. First, put the ASCII map inside a div with id="ascii-map" in your template. Then switch to the front-end. Once the DOM is fully loaded, you can parse the ASCII map, look for the asterisk, and then wrap it in a span element that has red color defined for its font.

In your CSS:

.red-font {
    color: red;
}

Then, some JQuery:

$(document).ready(function() {
    var text = $('#ascii-map').html();
    var textWithRed = text.replace("*", "<span class='red-font'>*</span>");
    $('#ascii-map').html(textWithRed);
});   

I test this and confirmed that it works.

Upvotes: 1

kddeisz
kddeisz

Reputation: 5182

Well... you're breaking MVC pretty badly, so that's one thing. Other than that though, do you need ERB for this? You're not embedding ruby at all. Why not just have it as a string?

i.e

def mark_character_coordinates
  WORLD.terrain[@character.y_coordinate][@character.x_coordinate][0][0] = "<span>*</span>"
end

Upvotes: 0

Related Questions