将格式化文本保存到数据库并将其“原样”检索回来,就像格式化字符串一样 [英] Save formatted text to database and retrieve it back 'as is' like a formatted string

查看:115
本文介绍了将格式化文本保存到数据库并将其“原样”检索回来,就像格式化字符串一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在twitter bootstrap表单中嵌入了tinymce [http://www.tinymce.com/],因此用户可以输入格式化文本,我可以将其保存到postgres数据库中。

I am embedding tinymce [http://www.tinymce.com/] in twitter bootstrap form , so that user can input formatted text which I can persist to a postgres database.

我面临的问题是我在提交表格时得到明文。有人可以帮我把格式化文本作为一个字符串,我可以坚持并回显到html页面。

The problem I am facing is that I get plain text on submit of the form. Can someone help me get the formatted text as a string that I can persist and echo to the html page .

这是我的代码[注意:它使用HAML]

Here is my code [Note:It uses HAML]

%html{:lang => "en"}
  %head
    %meta{:charset => "utf-8"}
        %link{:href => "//netdna.bootstrapcdn.com/twitter-bootstrap/2.2.2/css/bootstrap-combined.min.css", :rel => "stylesheet"}
    %script{:src => "//tinymce.cachefly.net/4.0/tinymce.min.js"}
    :javascript
      tinymce.init({selector:'textarea_format'});
%body
  .container
    %legend Post a New Job
    .well
      %form#signup.form-horizontal{:action => "submit", :method => "post"}
        %legend
        .control-group
          %label.control-label Title
          .controls
            .input-prepend
              %span.add-on
                %i.icon-user
              %input#title.input-xxlarge{:name => "title", :placeholder => "Title", :type => "text"}/
        .control-group
          %label.control-label Description Formatted
          .controls
            .input-prepend
              -#%span.add-on
                -#%i.icon-user
              %textarea_format#message.input-xlarge.span6{:name => "message_formatted", :rows => "10"}
        .control-group
          %label.control-label
          .controls
            %button.btn.btn-success{:type => "submit"} Submit 

好像我可以使用tinyMCE.get('content id')。getContent ()但我不确定如何?

Seems like I can use tinyMCE.get('content id').getContent() but I am not sure how ?

我是java-script的新手。有人可以发布所需的代码更改和一些解释。

I am very new to java-script . Can someone please post the required code change and some explanation.

谢谢

推荐答案

基本上,最简单的方法是通过向表单添加隐藏字段和在提交按钮上添加单击事件处理程序。

Basically, the simplest way to do this is through adding a hidden field to your form and a click event handler on the submit button.

因此,在您的格式上创建隐藏输入,名称为message_formatted(因为我想你的模型中你的相应字段叫做message_formatted),并且你的textarea的名字改为别的,因为这将不再重要。

so, create a hidden input on your format, with the name "message_formatted" (since I imagine your corresponding field in your model is called message_formatted), and change the name of your textarea to something else, as that will no longer be important.

使用jQuery :

$('#signup input[type=submit]').click(function(e){
  $('input[name=message_formatted]').val(tinyMCE.get('content id').getContent());
});

就添加javascript的位置而言,这取决于你。最好将它放在javascripts目录中(目前还不清楚你是否使用Rails和资产管道)。如果您只想在此haml页面中添加内嵌javascript,请将以上内容放在页面底部的 content_for:javascript do

In terms of where to add the javascript, that's up to you. It's best to place it in a javascripts directory (it's unclear whether you're using Rails and the asset pipeline or not). If you want to just add the javascript inline within this haml page, place the above within the content_for :javascript do at the bottom of your page

content_for :javascript do
  // enter the javascript from above here

这篇关于将格式化文本保存到数据库并将其“原样”检索回来,就像格式化字符串一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆