madebymany / sir-trevor-rails

Rails gem for Sir Trevor integration.
MIT License
149 stars 50 forks source link

no implicit conversion of Hash into String on create #33

Closed 42ae closed 9 years ago

42ae commented 9 years ago

I am using a very simple scenario here, but when calling the create action of my controller, I get the following error:

TypeError - no implicit conversion of Hash into String:
json (1.8.1) lib/json/common.rb:155:in `parse'
multi_json (1.10.1) lib/multi_json/adapters/json_common.rb:16:in `load'
multi_json (1.10.1) lib/multi_json/adapter.rb:20:in `load'
multi_json (1.10.1) lib/multi_json.rb:119:in `load'
 () usr/local/var/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/bundler/gems/sir-trevor-rails-acbaf9fee903/lib/sir_trevor_rails/block_array.rb:5:in `from_json'
 () usr/local/var/rbenv/versions/2.0.0-p353/lib/ruby/gems/2.0.0/bundler/gems/sir-trevor-rails-acbaf9fee903/lib/sir_trevor_rails/has_sir_trevor_content.rb:22:in `block in define_attribute_reader'

The create action was generated through scaffolding, my model does have sir_trevor_content :content and my view is a simple text_area field:

= simple_form_for @page, html: { multipart: true, id: "page-form" } do |f|
  = f.text_area :content, class: 'js-st-instance'
  .actions
    = f.button :submit
javascript:
  new SirTrevor.Editor({ el: $('.js-st-instance') });

Note that the controller action parameters seem to be correct:

{"utf8"=>"✓", "authenticity_token"=>"KVIL9KNWDnWXJTFrlrcqkhXkCcDearjS1khqLYL20us=", "page"=>{"content"=>"{\"data\":[{\"type\":\"text\",\"data\":{\"text\":\"Hello World\\n\"}}]}"}, "commit"=>"Create Page", "action"=>"create", "controller"=>"admin/pages", "locale"=>"en"}

The error occurs on save (@page = Page.new(page_params) if @page.save). Any idea? Thanks!

using rails 4.1.6

42ae commented 9 years ago

I fixed it by monkey-patching the gem:

def self.from_json(str, parent = nil)
  blocks = MultiJson.load(str.to_json, symbolize_keys: true)
  blocks = blocks[:data] if blocks.is_a?(Hash)
  blocks.map! { |block_obj| SirTrevorRails::Block.from_hash(block_obj, parent) }
  new blocks
end

Can anyone confirm that this bug so I can commit the fix?

lsirivong commented 9 years ago

@liyali do you happen to be using a data type in your database that would return a hash instead of a string, such as a json column in postgres? The gem appears to assume a string based data type to serialize between.

42ae commented 9 years ago

@lsirivong correct, I am using a postgres json column type.

dwb commented 9 years ago

@liyali Thanks for reporting! I've committed something that should fix this. Try it out and let us know how it goes.

Eiiki commented 8 years ago

This appears to be still broken. I'm using the sir_trevor_rails, v 0.5.0b1 gem.