views:

556

answers:

2

I have a nested attribute (speeches) under a model Speaker and I'm using tinyMce to let a speaker fill out a profile form where they might have one or more speeches they give.

I'm using the Rails 2.3 nested attributes helpers as used in Ryan Bates's complex-form-example github account.

The tinyMce functionality is great for a "Speech" if it's loaded by the page, but not active for a new "Speech" loaded by AJAX on "Add new speech" (calls a insert_fields script to add the form fields)

The tinyMce code on the page is

<script src="/javascripts/tiny_mce/tiny_mce_src.js?1254270151" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
tinyMCE.init({
editor_selector : 'mceEditor',
language : 'en',
mode : 'textareas',
theme : 'simple'

});
//]]>
</script>

And to activate a textarea form field, you put class="mceEditor" on it.

Is there a way to activate tinyMce on the new Ajax inserted form fields?

TinyMce plugin: http://github.com/kete/tiny%5Fmce

A: 

You can create a new editor after TinyMCE has been initialized using:

var new_editor = new tinymce.Editor('new_id', {
});
new_editor.render();

This won't utilize the settings you have in your controller, and is a pure javascript solution, not really a ruby one, but you should be able to easily run it after your ajax call completes and you have the id of the new text area.

danivovich
I added it to function insert_fields, but it didn't create the right effect.function insert_fields(link, method, content) { var new_id = new Date().getTime(); var regexp = new RegExp("new_" + method, "g") $(link).up().insert({ before: content.replace(regexp, new_id) }); var new_editor = new tinymce.Editor(new_id, { }); new_editor.render();}
Josh Crews
I'm not following what your function is doing. If "new_" + method is the ID of the newly added text area, just send that to the Editor constructor. I'm not sure passing the time in will actually work since it is expecting a string that is the ID of the text area in the DOM
danivovich
A: 

I don't know if this is a good answer; but for this situation (someone following the Railscast on nested attributes and complex forms and wanting to use tinyMCE)

pasting

<script type="text/javascript">
//<![CDATA[
tinyMCE.init({
editor_selector : 'mceEditor',
language : 'en',
mode : 'textareas',
theme : 'simple'

});
//]]>
</script>

Into my insert fields function made it worl

Josh Crews