如何将form_for与非输入元素一起使用?

时间:2014-11-14 21:08:02

标签: javascript jquery ruby-on-rails ajax ace-editor

我想在我的视图中使用Ace Editor来创建帖子对象,而不是textarea,但Ace Editor需要使用<div>来包含其内容。我是Ruby on Rails的新手4我试图找出如何使用Ace Editor安全地提交用户输入,并将用户重定向到显示帖子提交后的页面。这是我目前提交尚未使用Ace编辑器<div> ...

的帖子的表格
<%= form_for(@post) do |f| %>
  <div class="field">
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :description %><br>
     <!-- I'd like to replace this text_area with the ace editor div-->
    <%= f.text_area :description %> 
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

如何用<div>替换f.text_area元素并在表单的帖子请求中包含它的内容?

1 个答案:

答案 0 :(得分:1)

使用新版本的ace,您也可以使用textarea

请参阅http://jsbin.com/jodeyugenu/1/edit

&#13;
&#13;
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
  <script src="http://ajaxorg.github.io/ace-builds/src/ace.js"></script> 
  <style>
    #ta { position: absolute; top: 0; left: 0; right: 0; bottom: 0;}
  </style>
</head>
<body>
  <form id="myForm">
    <div style="position:relative;height:200px;width:300px">
      <textarea id="ta">text</textarea> 
    </div>
    <input type="submit" value="submit" >
  </form>
</body>
<script>
  var textareaEl = document.getElementById("ta");
  // update textarea value before submitting
  var form = textareaEl.form
  form.addEventListener("submit", function() {    
    textareaEl.style.visibility = "hidden"
    textareaEl.value = editor.getValue()
    form.appendChild(textareaEl)
  });
  // create editor and set id for it
  editor = ace.edit(textareaEl)
  editor.container.id = "ta"
  // add a keyboard shortcut for submitting
  editor.commands.addCommand({
    name: "submit",
    exec: function() {
      form.submit()
    },
    bindKey: "Ctrl-Enter"
  })
</script>
</html>
&#13;
&#13;
&#13;