マイ マーキュリー ジェム - バージョン 0.9.0
gem 'mercury-rails', git: 'https://github.com/jejacks0n/mercury.git'
Rails4を使用しています
もともと RailsCast に従っていましたが、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'...)」イベントは正常に発生します。
編集: わかりました。「保存された」イベントが 1 つのルートでは機能しているが、別のルートでは機能していないことがわかりました。
#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