2

I am setting up an API.

The client (using HTTParty) posts this to the API:

{:body => 
       {
        :product=> {:description=>"some text", :cost => "11.99"}, 
        :brand=>   {:name=>"BrandName", :etc =>"hey"}
       }
}

The server/api receives the post. Now, if I access params[:brand] I get:

{"name"=>"BrandName", "etc" =>"hey"}

If I do this:

Brand.new(params[:brand])

Then I get a new Brand object with the "name" and "etc" attributes populated correctly.

However, if I try to access params[:brand][:name], I just get nil

Any ideas?

Thanks.

1 Answer 1

3

Use params[:brand]["name"] or params["brand"]["name"]

Hash keys can be any sort of object. Common rails practice is to use symbols as hash keys, but when translated from JSON, the keys are likely to be strings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.