Hi, Regularly, you create a resource in html via form. you ve got some naming conventions for form fields and rails will bring it together for you in params hash. My question is, how my i do this with json. I would like to create a resource with pure json. do I have to follow specific naming,syntax convention also in order to get everything in my params hash ? Just a simple example, Im doing http post for player resource with following body: {"player": {"firstname":"Bob","lastname":"Alice"}} If I do a params.each do key, value in my create action I get my json as a key ??? -- Volker -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Aug 2, 2011 at 9:47 PM, vhochstein <vhochstein-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>wrote:> Hi, > > Regularly, you create a resource in html via form. you ve got some > naming conventions for form fields and rails will bring it together > for you in params hash. > > My question is, how my i do this with json. I would like to create a > resource with pure json. > > do I have to follow specific naming,syntax convention also in order to > get everything in my params hash ? > > Just a simple example, Im doing http post for player resource with > following body: > {"player": {"firstname":"Bob","lastname":"Alice"}} >This is the right format of the params to be posted. Before you post it, you need to encode it: JSON.encode({"player": {"firstname":"Bob","lastname":"Alice"}}) and then set Content-Type to application/json If I do a params.each do key, value in my create action> I get my json as a key ??? > > -- > Volker > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- Tower He -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks a lot. I copied code for index action to create and I was setting Accept header to applciation/json. Fixed that and it runs as expected. Great. :-) -- Volker On 2 Aug., 17:00, Tower He <towe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Tue, Aug 2, 2011 at 9:47 PM, vhochstein <vhochst...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>wrote: > > > > > > > > > > > Hi, > > > Regularly, you create a resource in html via form. you ve got some > > naming conventions for form fields and rails will bring it together > > for you in params hash. > > > My question is, how my i do this with json. I would like to create a > > resource with pure json. > > > do I have to follow specific naming,syntax convention also in order to > > get everything in my params hash ? > > > Just a simple example, Im doing http post for player resource with > > following body: > > {"player": {"firstname":"Bob","lastname":"Alice"}} > > This is the right format of the params to be posted. Before you post it, you > need to encode it: > > JSON.encode({"player": {"firstname":"Bob","lastname":"Alice"}}) > > and then set Content-Type to application/json > > If I do a params.each do key, value in my create action > > > I get my json as a key ??? > > > -- > > Volker > > > -- > > You received this message because you are subscribed to the Google Groups > > "Ruby on Rails: Talk" group. > > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to > > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group at > >http://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > Tower He-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.