Search code examples
ruby-on-rails-3.1mercury-editor

Can I use Mercury Editor without an specific Pages Controller?


I want to use Mercury Editor (https://github.com/jejacks0n/mercury) to edit posts in the blog area of my RoR application. I don't have any Pages model, I just want to edit posts and not all the site's pages.

In my routes.rb:

resources :posts do
    member { post :mercury_update }
end

In my posts_controller.rb:

def mercury_update
    post = Post.find(params[:id])
    post.name = params[:content][:page_name][:value]
    post.content = params[:content][:page_content][:value]
    post.save!
    render text: ""
end

And also did:

$(window).bind('mercury:ready', function() {
  var link = $('#mercury_iframe').contents().find('#edit_link');
  Mercury.saveURL = link.data('save-url');
  link.hide();
});

$(window).bind('mercury:saved', function() {
  window.location = window.location.href.replace(/\/editor\//i, '/');
});

And changed save style to 'form' in mercury.html.erb.

Even following those steps, I'm getting this error over and over when I try to save changes in a test post:

Mercury was unable to save to the url: http://localhost:3000/blog/test


Solution

  • I had this same problem too with the javascript not loading. I would suggest to consider putting the javascript overrides into the /app/assets/javascript/mercury.js inside the onload area. Here is what mine looks like:

    onload: function() {
    $(window).bind('mercury:ready', function() {
      var link = $('#mercury_iframe').contents().find('#edit_link');
      Mercury.saveURL = link.data('save-url');
      link.hide();
    });
    },
    

    Another option is to put the override code inside the file /app/assets/javascript/mercury_override.js, like so:

    $(window).bind('mercury:ready', function() {
      var link = $('#mercury_iframe').contents().find('#edit_link');
      Mercury.saveURL = link.data('save-url');
      link.hide();
    });
    

    I think the 2nd solution is a better one as this would not be touched by the any updates applied from the mercury updates.