【问题标题】:Write Ruby code inside TinyMCE textbox在 TinyMCE 文本框中编写 Ruby 代码
【发布时间】:2013-06-12 20:08:04
【问题描述】:

是否可以在 TinyMCE 文本框中编写 Ruby 代码?我尝试通过将 放入 TinyMCE 文本框来访问一些模型数据,但是在呈现原始文本时,它按原样显示 Ruby 代码 - 它不呈现 Ruby 代码,只是将其视为一部分文本。

我尝试了rawhtml_safe 方法,但都没有成功。

有什么建议吗?

编辑:我正在使用tinymce-rails gem,以下是我表单中的代码:

  <td><%= section_form.text_area :html, class: "tinymce", style: "width:800px; height:300px;" %></td>
  <%= tinymce content_css: asset_path('tiny_mce_content.css') %>

【问题讨论】:

  • 您如何尝试将文本添加到 tinymce? stackoverflow.com/questions/488189/… 可能是相关的。你在使用 tinyMCE.activeEditor.setContent 吗?
  • 我正在使用tinymce-rails gem。我用表单中的代码更新了问题。

标签: ruby-on-rails ruby tinymce erb


【解决方案1】:

只需通过 ERB 运行即可,例如:

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

如果您希望传入变量,请查看这篇文章,其中有几个选项:Ruby templates: How to pass variables into inlined ERB?

【讨论】:

  • 不幸的是,上面的代码显示的 Ruby 代码没有任何不同。它仍然没有返回对象 - 只是文本。我试过&lt;% foo = ERB.new("&lt;%= current_profile.name %&gt;").result %&gt; 然后显示 foo;我也尝试了&lt;% foo = ERB.new("&lt;%= current_profile.name %&gt;").result(namespace.instance_eval { binding }) %&gt;,然后显示 foo 但两种方式都给我一个语法错误:syntax error, unexpected keyword_class, expecting keyword_do or '{' or '('
  • @mmichael 是的,不幸的是,这不会让您访问当前的变量范围。你必须跳过一些障碍——看看我在答案中链接到的帖子。
  • 我会仔细看看。谢谢您的答复。我明天再试一次。
猜你喜欢
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多