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') %>
2
  • How are you trying to add your text to tinymce? stackoverflow.com/questions/488189/… may be relevant. Are you using tinyMCE.activeEditor.setContent? Commented Jun 12, 2013 at 20:27
  • I'm using the tinymce-rails gem. I updated the question with code from my form. Commented Jun 12, 2013 at 20:46

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

3 Comments

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 '('.
@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.
I'll take a closer look. Thank you for your response. I'll give it another go tomorrow.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.