簡體   English   中英

如何將form_for與非輸入元素一起使用?

[英]How do I use form_for with non-input elements?

我想在視圖中使用Ace編輯器來創建post對象,而不是文本區域,但是Ace編輯器需要使用<div>來包含其內容。 我是Ruby on Rails 4的新手,我試圖弄清楚如何使用Ace編輯器安全地提交用戶輸入,並將用戶重定向到提交后顯示該頁面的頁面。 這是我當前的表單,用於提交尚未使用Ace Editor <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元素並將其內容包含在表單的發布請求中?

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

參見http://jsbin.com/jodeyugenu/1/edit

 <!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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM