Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.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
Javascript 如何让Mercury Editor重定向到新资源?_Javascript_Ruby On Rails_Mercury Editor - Fatal编程技术网

Javascript 如何让Mercury Editor重定向到新资源?

Javascript 如何让Mercury Editor重定向到新资源?,javascript,ruby-on-rails,mercury-editor,Javascript,Ruby On Rails,Mercury Editor,观看之后,我试图让编辑器重定向到新创建的资源 我已经可以使用JS和window.location.href=在客户端重定向。但是对于一个新资源,我无法在客户端“猜测”它的URL。我需要它出现在服务器响应中 然而,问题是我不认为在编辑器中使用服务器响应的可能性。无论控制器呈现什么,服务器响应都由Mercury执行,而不是用作我的函数的参数,用于Mercury:saved 有没有办法解决这个问题?我可以在更新时通过发送一个有效的JSON字符串来解决这个问题。我会假设创建的方式是相同的。检查fireb

观看之后,我试图让编辑器重定向到新创建的资源

我已经可以使用JS和
window.location.href=
在客户端重定向。但是对于一个新资源,我无法在客户端“猜测”它的URL。我需要它出现在服务器响应中

然而,问题是我不认为在编辑器中使用服务器响应的可能性。无论控制器呈现什么,服务器响应都由Mercury执行,而不是用作我的函数的参数,用于
Mercury:saved


有没有办法解决这个问题?

我可以在更新时通过发送一个有效的JSON字符串来解决这个问题。我会假设创建的方式是相同的。检查firebug以确保Mercury使用的jQuery.ajax调用中没有出现错误

posts\u 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
mercury.js:

  jQuery(window).on('mercury:ready', function() {
    Mercury.on('saved', function() {
       window.location.href = arguments[1].url      
    });
  });

注意:我在服务器端使用slug-my-posts重定向不起作用,因为save按钮只是一个
jQuery.ajax
调用:

// page_editor.js
PageEditor.prototype.save = function(callback) {
      var data, method, options, url, _ref, _ref1,
        _this = this;
      url = (_ref = (_ref1 = this.saveUrl) != null ? _ref1 : Mercury.saveUrl) != null ? _ref : this.iframeSrc();
      data = this.serialize();
      data = {
        content: data
      };
      if (this.options.saveMethod === 'POST') {
        method = 'POST';
      } else {
        method = 'PUT';
        data['_method'] = method;
      }
      Mercury.log('saving', data);
      options = {
        headers: Mercury.ajaxHeaders(),
        type: method,
        dataType: this.options.saveDataType,
        data: data,
        success: function(response) {
          Mercury.changes = false;
          Mercury.trigger('saved', response);
          if (typeof callback === 'function') {
            return callback();
          }
        },
        error: function(response) {
          Mercury.trigger('save_failed', response);
          return Mercury.notify('Mercury was unable to save to the url: %s', url);
        }
      };
      if (this.options.saveStyle !== 'form') {
        options['data'] = jQuery.toJSON(data);
        options['contentType'] = 'application/json';
      }
      return jQuery.ajax(url, options);
    };
因此,重定向被发送到
success
回调,但页面实际上不会像任何成功的AJAX请求那样重新呈现。作者讨论了重写这个函数。通过将回调函数传递给
save
,这里似乎还有一些回旋余地

顺便说一句,@corneliusk建议的另一种方法是:

render { json: {url: post_path(post.slug)} }

无论哪种方式,响应主体都会作为参数传递给
mercury:saved
回调中的函数。

我想你的意思是
参数[1]。上面的url
。@jordanpg,我想你的意思是
render:json=>{:url=>post\u路径(post.slug)}
below@awendt-似乎这个答案对人们很有用,你认为你能接受吗?