All threads / Saved messages as HTML

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Saved messages as HTML

Martin McDermid asked in General

Hi Chris,

I'm wondering, on your episode https://gorails.com/episodes/message-templates, would it be possible to have one of the saved messages as HTML? I've tried entering <h1>test</h1><p>this is a test</p> but nothing appears once selected (just some whitespace). Plain text works fine.

This is what I see in the source:

        <option data-body="&lt;h1&gt;test&lt;/h1&gt;&lt;br&gt;&lt;p&gt;this is a test&lt;/p&gt;" value="1">Test</option>

It's obviously formatting it, but am I able to disable this?

Thanks Chris, I appreciate it.

Hey Martin!

This is actually a small bug you find in the JS for that episode. The append() in handleChange works fine for adding plain strings, but not HTML ones to the value of the inputs. If you change the append line to the following, you'll see that it works just fine:

    comment_body.val comment_body.val() + saved_message_text

This is a better way of adding text to a field, where you set the value to the old value plus your new text. Append seems to do that, but not when there is html that you want to add for some reason.

I updated the repository to fix that bug as well. 👍

That's brilliant Chris, works perfectly! :) Thanks very much. Been playing around with it all night but I'm quite the novice when it comes to JS!

Yeah, that was one of those weird things that jQuery does. It'll work as you might think, but not completely. If you were writing this with plain JS, you'd use the same code as I did in my solution basically. No funny business that way.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 37,344+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.