Save the formatted text to the database and return it back as is, as a formatted string

I embed tinymce [http://www.tinymce.com/] in twitter bootstrap form so that the user can enter formatted text that I can save in the postgres database.

The problem I am facing is that I get plain text when submitting the form. Can someone help me get the formatted text as a string that I can save and repeat on the html page.

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 

It looks like I can use tinyMCE.get ('content id'). getContent (), but I'm not sure how?

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

thank

+1
javascript web-applications postgresql forms tinymce
Jun 22 '13 at 6:14
source share
1 answer

Basically, the easiest way to do this is to add a hidden field to the form and an event handler for clicking on the submit button.

therefore, create a hidden input in your format named "message_formatted" (since I assume that your corresponding field in your model is called message_formatted) and change the name of your text field to something else, since it will no longer be important.

using jQuery:

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

In terms of where to add javascript, it is up to you. It is better to put it in the javascripts directory (it is unclear whether you use Rails and the asset pipeline or not). If you just want to add a javascript line inside this haml page, put above in content_for :javascript do at the bottom of the page

 content_for :javascript do // enter the javascript from above here 
+1
Jul 10 '13 at 13:20
source share



All Articles