2

I am adding mercury to a simple blogging application and would like to use the newest version, but I have not been able to get it to save updates. Here is my code:

Gemfile:

gem 'rails', '3.2.11'
gem "thin", "~> 1.3.1"
gem 'mercury-rails'
gem 'paperclip'

group :development do
  gem 'taps'
  gem "nifty-generators"
  gem 'sqlite3'
end

gem 'jquery-rails'

blogs_controller.rb:

  def update
    @blog = Blog.find(params[:id])
    if @blog.update_attributes(params[:blog])
      redirect_to @blog
    else
      render 'edit'
    end
  end

  def mercury_update
    blog = Blog.find(params[:id])
    blog.content         = params[:content][:blogContent][:value]
    blog.save!
    render text: ""
  end

blog.rb:

class Blog < ActiveRecord::Base
  attr_accessible :title, :content, :author
end

routes.rb:

  Mercury::Engine.routes
  mount Mercury::Engine => '/'
  root :to => "pages#home"

  namespace :mercury do
    resources :images
  end

  resources :blogs do
    member { post :mercury_update }
  end

mercury.html.erb

  <body>
    <script type="text/javascript">
      // Set to the url that you want to save any given page to, leave null for default handling.
      var saveUrl = null;

      // Instantiate the PageEditor
      new Mercury.PageEditor(saveUrl, {
        saveStyle:  'form', // 'form', or 'json' (default json)
        saveMethod: null, // 'PUT', or 'POST', (create, vs. update -- default PUT)
        visible:    true  // boolean - if the interface should start visible or not
      });
    </script>
  </body>

blogs/show.html.erb:

 <div id="blogFull" class="rounded-corners-mild">

 <div id="blogTitle">
   <h1 style="font-size:150%; text-align:center; " class="mercury-region" ><%= @blog.title%></h1>
  </div>

  <div data-mercury="full" id="blogContent" class="mercury-region">
    <%= raw @blog.content %>
  </div><br />
  <%= link_to "mercury", "/editor" + "/blogs" + "/#{@blog.id}", :class => 'bestButton',  
          id: "edit_link", data: {save_url: mercury_update_blog_path(@blog)} %>

added to: mercury.js:

jQuery(window).on('mercury:ready', function() {
        var saveUrl = $("#edit_link").data("save-url");
        Mercury.saveUrl = saveUrl;
});
Mercury.on('saved', function() {
        window.location.href = window.location.href.replace(/\/editor\//i, '/');
});

I also tried with this: to mercury.js:

  onload: function() {
    //Mercury.PageEditor.prototype.iframeSrc = function(url) { return '/testing'; }
    Mercury.on('ready', function() {
      var link = $('#mercury_iframe').contents().find('#edit_link');
      Mercury.saveUrl = link.data('save-url');
      link.hide();
    });

    Mercury.on('saved', function() {
      window.location.href = window.location.href.replace(/\/editor\//i, '/');
    });
  }

Neither worked. Both got me to the editor and allowed me to edit. I have not been able to save any changes yet. Thank You.

thatdankent
  • 950
  • 8
  • 15

2 Answers2

3

Do you get an error message when trying to save? What does it say? If it says it's unable to save but the URL is correct you want to check your log file.

I had problems setting the correct saveUrl and adding the following to mercury.js fixed it for me;

onload: function() {
    Mercury.on('ready', function() {
        var link = $('#mercury_iframe').contents().find('#edit_link');
        console.log("mercury ready ready", link);
        mercuryInstance.saveUrl = link.data('save-url');
        link.hide();
    });

    Mercury.on('saved', function() {
        window.location.href = window.location.href.replace(/\/editor\//i, '/');
    });
}

Found it in this thread on RailsCasts.

Edit:

I notice something else in your code. You created a route for a POST action. In mercury.html.erb the saveMethod gets set to 'PUT' by default. Change the saveMethod to 'POST'.

Johan Baaij
  • 584
  • 1
  • 5
  • 15
  • Yes. Now that I changed my save method to POST I get an error. "RoutingError (No route matches [POST] "/blogs/3")", but it is clearly in the routes file as a Post route. Any idea how to fix it? – thatdankent Jan 16 '13 at 22:57
  • @thatdankent Can you let me know how you fixed this? Cheers. – Ribena Dec 18 '13 at 04:24
3

For me I had trouble with "mercuryInstance" not being around with the solution above. I did get it to work with this javascript added to the end of mercury.js:

    jQuery(window).on('mercury:ready', function() {
        var link = $('#edit_link');
        Mercury.saveUrl =link.attr('data-save-url');
        link.hide();
    });