1

Is it possible to write Ruby code inside the TinyMCE textbox? I've tried accessing some model data by putting <%= foo.bar %> inside a TinyMCE textbox but when rendering the raw text it displays the Ruby code as is - it doesn't render the Ruby code, just treats it as part of the text.

I've tried the raw and html_safe methods but neither worked.

Any suggestions?

Edit: I'm using the tinymce-rails gem and the following is the code in my form:

  <td><%= section_form.text_area :html, class: "tinymce", style: "width:800px; height:300px;" %></td>
  <%= tinymce content_css: asset_path('tiny_mce_content.css') %>
vich
  • 11,836
  • 13
  • 49
  • 66
  • How are you trying to add your text to tinymce? http://stackoverflow.com/questions/488189/how-to-set-the-initial-text-an-a-tinymce-textarea may be relevant. Are you using tinyMCE.activeEditor.setContent? – Puhlze Jun 12 '13 at 20:27
  • I'm using the `tinymce-rails` gem. I updated the question with code from my form. – vich Jun 12 '13 at 20:46

1 Answers1

1

Just run it through ERB, e.g:

ERB.new("<%= 'something'.upcase %>").result
=> "SOMETHING" 

If you're looking to pass variables in, check out this post, which has a few options: Ruby templates: How to pass variables into inlined ERB?

Community
  • 1
  • 1
Michael Lawrie
  • 1,534
  • 11
  • 20
  • Unfortunately the code above doesn't display the Ruby code any different. It's still not returning the object - just the text. I tried `<% foo = ERB.new("<%= current_profile.name %>").result %>` then displaying foo; I also tried `<% foo = ERB.new("<%= current_profile.name %>").result(namespace.instance_eval { binding }) %>` and then displaying foo but both ways give me a syntax error: `syntax error, unexpected keyword_class, expecting keyword_do or '{' or '('`. – vich Jun 12 '13 at 21:17
  • @mmichael Yes, unfortunately this doesn't get you access to the current variable scope. You have to jump through some hoops-- take a look at the post I linked to in the answer. – Michael Lawrie Jun 12 '13 at 21:21
  • I'll take a closer look. Thank you for your response. I'll give it another go tomorrow. – vich Jun 12 '13 at 21:25