siteleaf / siteleaf-gem

Command-line interface (CLI) and Ruby interface for the Siteleaf API
http://siteleaf.com
MIT License
77 stars 11 forks source link

undefined method `strip' for ActiveSupport::HashWithIndifferentAccess #46

Closed TheCorp closed 9 years ago

TheCorp commented 9 years ago

For some reason when I apply a taxonomy I can't save a Post for a 2nd time. I am not sure if this is because of some specific issue using siteleaf-gem with Rails (4.1.9) and ActiveSupport but it seems very odd.

Here is an example using the taxonomy suggested in the main docs.

  siteleaf_post = Siteleaf::Post.create({
    parent_id: page.id,
    title: "some title",
    body: "some body",
    taxonomy: [ {"key" => "Tags", "values" => ["One","Two","Three"]} ]
  })

This outputs correctly and saves with no issues, it returns the new Post object with no errors. If I then try and just re-save the exact same siteleaf_post variable, I get the following:

irb(main):754:0> siteleaf_post.save
=> #<Siteleaf::Post:0x007f9061a90940 .... @taxonomy=[{"id"=>"55cd4ef269702d7fa1000071", "key"=>"Tags", "slug"=>"tags", "values"=>[{"value"=>"One", "slug"=>"one", "url"=>"/marketplace/tags/one"}, {"value"=>"Two", "slug"=>"two", "url"=>"/marketplace/tags/two"}, {"value"=>"Three", "slug"=>"three", "url"=>"/marketplace/tags/three"}]}], @error={"code"=>"uncaught_error", "message"=>"undefined method `strip' for {\"value\"=>\"One\", \"slug\"=>\"one\", \"url\"=>\"/marketplace/tags/one\"}:ActiveSupport::HashWithIndifferentAccess"}>

Any ideas?

sskylar commented 9 years ago

When creating your post, Siteleaf will return additional information:

"values"=>[
  {"value"=>"One", "slug"=>"one", "url"=>"/marketplace/tags/one"}, 
  {"value"=>"Two", "slug"=>"two", "url"=>"/marketplace/tags/two"}, 
  {"value"=>"Three", "slug"=>"three", "url"=>"/marketplace/tags/three"}
]

This information cannot be saved back as-is.

You'll want to do something like this instead:

post.taxonomy = [ {"key" => "Tags", "values" => ["One","Two","Three"]} ]
post.save

Also as a tip: If you only need to update one value (e.g. Title), you can do something like this to leave taxonomy and everything else unchanged:

post = Siteleaf::Post.new(id: 'abc123'), #where abc123 is existing post id
post.title = 'New Title'
post.save
TheCorp commented 9 years ago

Makes sense now!

Shouldn't the object be returned in a state that can be re-saved as is? Sorta breaks the whole Siteleaf site/page/post object concept in the gem.

sskylar commented 9 years ago

Arguably yes, but that's how the V1 API works. You're also going to get better performance by only saving changes, so that's our recommendation. That said, we're working on a V2 behind the scenes which will improve many things including this. More to come!

TheCorp commented 9 years ago

:+1: Duly noted :)