Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Mercury编辑器“已保存”事件未触发_Ruby On Rails_Ruby On Rails 4_Content Management System_Mercury Editor - Fatal编程技术网

Ruby on rails Mercury编辑器“已保存”事件未触发

Ruby on rails Mercury编辑器“已保存”事件未触发,ruby-on-rails,ruby-on-rails-4,content-management-system,mercury-editor,Ruby On Rails,Ruby On Rails 4,Content Management System,Mercury Editor,我的Mercury Gem-版本0.9.0 gem 'mercury-rails', git: 'https://github.com/jejacks0n/mercury.git' 我用的是铁轨 最初,我在Mercury.js中有以下内容: $(window).bind('mercury:ready', function() { var link = $('#mercury_iframe').contents().find('#edit_link');//extract the saveU

我的Mercury Gem-版本0.9.0

gem 'mercury-rails', git: 'https://github.com/jejacks0n/mercury.git'
我用的是铁轨

最初,我在Mercury.js中有以下内容:

$(window).bind('mercury:ready', function() {
  var link = $('#mercury_iframe').contents().find('#edit_link');//extract the saveURL for mercury that was encoded in the HTML data tag
  Mercury.saveUrl =link.data('save-url');
  console.log("Mercury save URL: "+Mercury.saveUrl);
  link.hide();//hide edit url
});

$(window).bind('mercury:saved', function() {
  console.log("mercury saved event set");
  window.location.href = window.location.href.replace(/\/editor\//i, '/');
});
但是,RailsCast评论说,现在应该覆盖onload函数:

window.Mercury = {
  //...

  //CUSTOM CODE
  onload:function(){
    Mercury.on('saved',function(){
        console.log("SAVE EVENT FIRED");
        window.location.href = window.location.href.replace(/\/editor\//i, '/');            
    });
    Mercury.on('ready',function(){
        var link = $('#mercury_iframe').contents().find('#edit_link');//extract the saveURL that was encoded in the HTML data tag
        Mercury.saveUrl =link.data('save-url');
        console.log("Mercury save URL: "+Mercury.saveUrl);
        link.hide();//hide edit url
    });
  }
};
但是,保存的事件永远不会触发,并且重定向也永远不会发生。你知道新的改进方法是什么吗?谢谢大家!

编辑:为了澄清,“Mercury.on'ready'…”事件确实成功触发

编辑:好的,我发现“保存”事件使用的是一条路线,而不是另一条

#Works on this route! 
def mercury_update_courses
    courses_infos = params[:content]
    courses_infos.each{|course_info|
        label = course_info[0]
        new_val = course_info[1][:value]

        id = label.delete("^0-9")#find the database id by removing everything that's not a number from the HTML element's CSS id
        course = EditablePage.where("id == ?",id).last

        if label.index("title") != nil
            course.title = new_val
        elsif label.index("body") != nil
            course.body = new_val
        end

        course.save!
    }
    render text: ""
end

#does not work on this route!
def mercury_update_index
    homepage = EditablePage.find(params[:id])
    homepage.body = params[:content][:homepage_content][:value]
    homepage.save!
    render text: ""
end

发现了问题!“保存”事件实际上还是在工作。

console.log("SAVE EVENT FIRED");
没有出现,因为它被放置在重定向之前,重定向会擦除JS控制台。当我发现它在一个页面上工作而不是在另一个页面上工作时,我试图找出这些页面的不同之处,并发现问题出在URL中

#working
  http://localhost:3000/editor/courses
#not working
  http://localhost:3000/editor
因此,问题在于我从随后的教程中复制并粘贴的神秘正则表达式

window.location.href = window.location.href.replace(/\/editor\//i, '/');
他是罪魁祸首。非工作编辑器位于站点的根目录/主页上,因此在url的“/editor/”部分中没有包含第二个/。我通过制作一个更一般、更容易理解的替换行来解决这个问题

window.location.href = window.location.href.replace("/editor", '');
最终代码:

window.Mercury = {
  //...

  //CUSTOM CODE
  onload:function(){
    Mercury.on('saved',function(){
        window.location.href = window.location.href.replace("/editor", '');
        console.log("Mercury info saved!"); 
    });
    Mercury.on('ready',function(){
        var link = $('#mercury_iframe').contents().find('#edit_link');//extract the saveURL that was encoded in the HTML data tag
        Mercury.saveUrl =link.data('save-url');
        console.log("Mercury save URL: "+Mercury.saveUrl);
        link.hide();//hide edit url when mercury is open
    });
  }
};