1

I use action_text with a model and need to type in links using liquid template variables, for example:

Contact us at: <a href="mailto:support@{{website.domain_name}}">support@{{website.domain_name}}</a>

However action_text breaks these links by encoding the curly brackets :( it also does it in src img tags src attributes :(

Contact us at <a href="mailto:support@%7B%7Bwebsite.domain_name%7D%7D">support@example.com</a>

It does the same if I use my own tags eg [domain_name] anywhere in href or src attributes.

I just want my HTML to be left alone, untouched, exactly as I enter it, I don't need any "help" encoding URLs tfank you very much action_text/Rails.

Is there a way to fix or disable this unwanted behavior?

Later edit:

Turns out this trix thing is also removing any other HTML such as tables, divs and so on :( it even removes HTML data- attributes :(( how do I disable this? I just want a friendly HTML editor with basic functions (bold, underline, paragraphs, headings...)

Nick M
  • 2,424
  • 5
  • 34
  • 57

2 Answers2

0

You can use CGI. Example, If you have a Post model with has_rich_text :content, then In your show views, you can do this:

<%= (CGI.unescapeHTML @post.content.to_s).html_safe %>

Trix uses ActionText, it's own Rich text, so you first convert it to string with to_s, then encode the HTML stuff with CGI, then finally make the whole string html_safe to show in browser.

More context: No matter what you type in ActionText or text_area, back in the database everything will be stored as string. Databases don't save HTML. So you'll want to save HTML as a string only, then after receiving it in views, you can perform encoding and stuff to convert that back to HTML.


You can even use HTML Entities or Nokogiri(a bit complex)

cdadityang
  • 523
  • 2
  • 10
0

I use CGI::unescape

In my model I have something like:

class Post
   has_rich_text :content

   def decoded_content
      CGI::unescape(content.to_s)
   end
end

Then use it on posts objects. i.e. Post.first.decoded_content


CGI::unescapeHTML or CGI::unescape_html did not work for me.

Sandip Mane
  • 1,420
  • 1
  • 9
  • 14