2

我正在将水星添加到一个简单的博客应用程序中,并想使用最新版本,但我无法获得它来保存更新。这是我的代码:

宝石文件:

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

博客.rb:

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

路线.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

水星.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>

博客/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)} %>

添加到: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, '/');
});

我也试过这个:到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, '/');
    });
  }

都没有奏效。两者都让我去找编辑,并允许我编辑。我还不能保存任何更改。谢谢你。

4

2 回答 2

3

对我来说,我遇到了“mercuryInstance”无法使用上述解决方案的问题。我确实让它与添加到mercury.js末尾的这个javascript一起工作:

    jQuery(window).on('mercury:ready', function() {
        var link = $('#edit_link');
        Mercury.saveUrl =link.attr('data-save-url');
        link.hide();
    });
于 2013-02-21T19:17:28.890 回答
3

尝试保存时是否收到错误消息?它说什么?如果它说它无法保存但 URL 正确,您需要检查您的日志文件。

我在设置正确的 saveUrl 并将以下内容添加到mercury.js 时遇到问题,为我修复了它;

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, '/');
    });
}

在 RailsCasts 上的这个线程中找到它。

编辑:

我注意到您的代码中还有其他内容。您为 POST 操作创建了一个路由。在mercury.html.erb 中,saveMethod 默认设置为“PUT”。将 saveMethod 更改为“POST”。

于 2013-01-16T21:37:54.713 回答