jejacks0n / mercury

Mercury Editor: The Rails WYSIWYG editor that allows embedding full page editing capabilities directly inline.
http://jejacks0n.github.com/mercury
Other
2.63k stars 531 forks source link

Mercury always returns "unable to save" even with 200 OK #226

Closed JeanMertz closed 12 years ago

JeanMertz commented 12 years ago

This seems related to #155 but somehow I cannot get the proposed solution in that thread to work for me.

Here is how I respond to the POST request from Mercury:

  def create
    status = :ok

    params[:content].each do |block, options|
      @page = current_website.pages.find(params[:page_id])
      @block = @page.blocks.find_by_data_attribute(block)

      content = CGI.unescapeHTML(options[:value])
      if @block.content == content || @block.update_attributes(content: content)
        # everything went fine
      else
        status = :bad_request
      end
    end

    render text: '{}', status: status

The last line, the rendering part, is what was advised in that thread. However, Mercury still says something went wrong, even though the record(s) got updated and the response code clearly shows 200 OK.

Other things I tried:

render nothing: true, content_type: 'text/html', status: status
head(status)

Everything I try results in the same (expected) response:

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
X-UA-Compatible: IE=Edge
ETag: "99914b932bd37a50b983c5e7c90ae93b"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 4c25fdb76c6cf8d81dcbbbb2c2d88770
X-Runtime: 0.337953
Connection: close

But still, the popup shows up alerting me that something went wrong. (also the debugger indeed shows ["save_failed", Object])

gvarela commented 12 years ago

these types of errors are almost always jquery trying to parse the content as a different content type than what it is getting (or thinks it's getting). can you provide the jquery ajax error message? it's in the object reference in the last line of your issue. https://github.com/jejacks0n/mercury/blob/master/vendor/assets/javascripts/mercury/page_editor.js.coffee#L217

JeanMertz commented 12 years ago

You are correct. The problem was that I use the (deprecated?) mercury_loader.js to load Mercury in the current URL without prepending /editor or anything. The way I loaded this was through

javascript_include_tag "mercury_loader.js?saveStyle=form&saveDataType=script"

This worked pre Rails 3.1.x but after upgrading RailsI started to get the above error. Turns out Sprockets now appends .js to that line, resulting in a saveDataType of script.js which jQuery obviously doesn't understand.

Appending a quick & to the end of the above line fixed the problem for me. Thanks for the help.

gvarela commented 12 years ago

Not sure if the loader is deprecated. I know it has a lot of issues. I personally don't use it.