Holy cow, I just blew nearly an entire day trying to fix one stupid little problem with FCKEditor and remote_form_for. Had a lot of trouble finding the right answer online anywhere, so now that I've got it figured out, I'm posting here so hopefully the next person won't have as much trouble as I did.

I was trying to use FCKEditor in one field on a form, and wanted the form to submit to the controller via AJAX, which would respond with an RJS call to update the page. However, I found that when I clicked "Submit", the FCK field in params was blank, BUT if I clicked submit TWICE in a row, the second one sent all the correct data. Apparently, there's a similar problem with ASP.Net and using the RequiredFieldValidator, so all my searches kept leading me to answers to THAT problem, but I ain't using ASP or RequiredFieldValidator.

Eventually, I finally stopped and actually read all of the listings on the FCKEditor Wiki Troubleshooting page and found this post, which exactly described the problem. However, I'm not a big fan of ridiculously vague code with names like MyClass and MyObject... that's just not the Rails way. So... here's my variation on a theme.

First, Add this to your application.js...

var FCKFixer = {
  fixIt: function() {
    for ( i = 0; i < parent.frames.length; ++i )
      if ( parent.frames[i].FCK )
        parent.frames[i].FCK.UpdateLinkedField();
  }
}

Then add :onclick => "FCKFixer.fixIt();" as an argument to your submit tag. So, now your entire form should look something like...

<% remote_form_for :comment, @comment, :url => post_comments_path(@post) do |f| %>
    <%= f.date_select :birthday %>
    <%= f.select :flag, [['You Rock',:rocks],["You're Great",:great],['Can I Have Your Babies?',:have_babies_request]] %>
    <%= fckeditor_textarea :comment, :body %>
    <%= submit_tag 'Submit', :onclick => "FCKFixer.fixIt();" %>
<% end %>

That's it? Yes! Really? Yes! Have a nice day and see you next time.

Comments:
Great!
Posted By: Fernando Lages on 13 Sep 2007 22:05
Just what I needed! I was facing the same problem, just placed your code and now everything is working nice.
Thank you very much!
Works for Pylons WebHelpers as well
Posted By: Alex Marandon on 06 Aug 2008 03:35
I'm using the Pylons framework which is a Python Web framework including a port of the Ruby on Rails helpers. Your fix worked for me as well, thank you so much for sharing!

The only trivial change is to remember to use Python syntax when adding the onclick handler on the submit button ;-)

h.submit('Save', onclick="FCKFixer.fixIt();")
[add comment]