2012-07-17 3 views
4

RailsCast #296 about Mercury Editor을 본 후에 새로 만든 리소스로 리디렉션되도록 편집기를 가져 오려고합니다.Mercury Editor에서 새 리소스로 리디렉션되도록하려면 어떻게합니까?

JS 및 window.location.href=을 사용하여 클라이언트 쪽에서 이미 리디렉션 할 수 있습니다. 그러나 새로운 리소스의 경우 클라이언트 측에서 URL을 "추측"할 수 없습니다. 서버 응답에 있어야합니다.

그러나 문제는 편집기에서 서버 응답을 사용할 수 없다는 것입니다. 컨트롤러가 무엇을 렌더링하든 상관없이 서버 응답은 mercury:saved에 대한 내 함수의 인수로 사용되는 대신 Mercury의 discarded입니다.

이 문제를 해결할 방법이 있습니까?

답변

7

유효한 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 

mercury.js :

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

참고 : 내 글 때문에 작동하지 않습니다 서버 측

+0

위의 인수 [1] .url을 의미한다고 생각합니다. – jordanpg

+2

@jordanpg, 당신이'render : json => {: url => post_path (post.slug)}'를 의미한다고 생각합니다. – corneliusk

+0

@ awendt-이 답변은 사람들에게 유용 할 것 같아요, 받아 들일 수 있다고 생각하세요? – corneliusk

1

재 지정을 강타 할 friendly_id을 사용하고 있습니다 저장 버튼은 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 요청과 마찬가지로 다시 렌더링되지 않습니다. 저자는 바로이 기능을 무시하는 것에 대해 논의합니다 here. 또한 콜백 함수를 save에 전달하여 여기를 조종 할 여지가있는 것처럼 보입니다.

, BTW 제안 무엇 @corneliusk 할 수있는 또 다른 방법은 다음과 같습니다

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

어느 쪽이든는 응답 본문은 mercury:saved 콜백 함수에 인수로 전달됩니다.