awe*_*ndt 5 javascript ruby-on-rails mercury-editor
在观看关于Mercury Editor的RailsCast#296后,我试图让编辑器重定向到新创建的资源.
我已经可以使用JS和window.location.href=.在客户端重定向.但是对于新资源,我不能在客户端"猜测"它的URL.我需要它在服务器响应中.
但是,问题是我没有看到在编辑器中使用服务器响应的可能性.无论控制器呈现什么,Mercury 都会丢弃服务器响应,而不是将其用作我的函数的参数mercury:saved.
有办法解决这个问题吗?
通过发送有效的JSON字符串,我能够在更新时执行此操作.我认为创建工作方式相同.检查firebug以确保您没有在Mercury使用的jQuery.ajax调用中收到错误.
posts_controller.rb
def mercury_update
post = Post.find(params[:id])
post.title = params[:content][:post_title][:value]
post.body = params[:content][:post_body][:value]
post.save!
render text: '{"url":"'+ post_path(post.slug) +'"}'
end
Run Code Online (Sandbox Code Playgroud)
mercury.js:
jQuery(window).on('mercury:ready', function() {
Mercury.on('saved', function() {
window.location.href = arguments[1].url
});
});
Run Code Online (Sandbox Code Playgroud)
注意:我正在使用friendly_id来发布帖子