Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined method `delete' for true:TrueClass when executing destroy #89

Closed
hamann opened this issue Apr 2, 2013 · 6 comments
Closed

Comments

@hamann
Copy link

hamann commented Apr 2, 2013

This is what I get when I execute 'destroy' on a resource. The resource itself is deleted afterwards.

>> j = PostgresRecoverJob.find("6e3421a41aacd3354c9fb9e7f1d0c854")
=> #<PostgresRecoverJob(recover_jobs/6e3421a41aacd3354c9fb9e7f1d0c854) status="queued" time="2013-04-02 22:04:03 +0200" message="" pct_complete=0 server="sauspiel" target="localhost" backup_id="20130402T171257" completed_at="" id="6e3421a41aacd3354c9fb9e7f1d0c854">
>> j.destroy
NoMethodError: undefined method `delete' for true:TrueClass
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/her-0.5.2/lib/her/middleware/first_level_parse_json.rb:11:in `parse'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/her-0.5.2/lib/her/middleware/first_level_parse_json.rb:29:in `on_complete'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/faraday-0.8.6/lib/faraday/response.rb:9:in `block in call'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/faraday-0.8.6/lib/faraday/response.rb:63:in `on_complete'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/faraday-0.8.6/lib/faraday/response.rb:8:in `call'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/faraday-0.8.6/lib/faraday/request/url_encoded.rb:14:in `call'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/faraday-0.8.6/lib/faraday/connection.rb:247:in `run_request'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/faraday-0.8.6/lib/faraday/connection.rb:100:in `delete'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/her-0.5.2/lib/her/api.rb:88:in `request'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/her-0.5.2/lib/her/model/http.rb:24:in `request'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/her-0.5.2/lib/her/model/orm.rb:177:in `block in destroy'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:403:in `_run__1606273922648791830__destroy__1484856305865657010__callbacks'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:405:in `__run_callback'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:385:in `_run_destroy_callbacks'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.13/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/her-0.5.2/lib/her/model/orm.rb:176:in `destroy'
    from (irb):2
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
    from /Users/hamann/workspace/sauspiel/sauspiel-rails/vendor/bundle/ruby/1.9.1/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>
@remi
Copy link
Owner

remi commented Apr 2, 2013

What is the response sent back from the API? The only way you could get this error is if the response body would be true:

json = MultiJson.load('true')
json.delete(:errors) # raises NoMethodError: undefined method `delete' for true:TrueClass

I actually wanted to implement some kind of check for this over the weekend to make sure this error doesn’t happen because I thought that only objects or arrays could be sent back from MultiJson.load. But it looks like simple values like true or "foo" are actually valid JSON data.

Anyway, making the endpoint called by #destroy return something like {} for now should do the trick for now. But I’ll come up with something better soon.

@remi
Copy link
Owner

remi commented Apr 2, 2013

FWIW, I just pushed a commit in master (30584da) that should provide a better error message when encountering this problem. Will be part of the next release.

@hamann
Copy link
Author

hamann commented Apr 2, 2013

You're right, the response is true

Is there a best practice how such a response message should look like?

@remi
Copy link
Owner

remi commented Apr 2, 2013

By default, if the server returns an empty hash ({} or { "model_name": {} }, depending on whether you’re using the parse_root_in_json setting), it will keep the model attributes intact and just mark it as destroyed (calling #destroyed? on it will return true).

In my opinion, a good practice would be to return an empty response and send it using the 204 No Content HTTP header. Her will behave the same way as described above.

@remi
Copy link
Owner

remi commented Apr 3, 2013

I’m going to close this issue now. Please let me know if you have further questions!

@remi remi closed this as completed Apr 3, 2013
@hamann
Copy link
Author

hamann commented Apr 3, 2013

Sorry, but I haven't got time yet to test it. But the case is clear to me and I will change the response messages. Thanks for your work and help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants