G-money
2013-Jul-06 20:14 UTC
I thought Rails 3.2 ActionController automatically converts JSON params hashes...
Hey, I was thinking I''d have my controllers do double-duty: the same actions handling json and html requests differentially. For this, it''s useful that Rails 3.2 automatic params hash conversion from json work (as described here<http://guides.rubyonrails.org/v3.2.13/action_controller_overview.html#json-xml-parameters>in the Rails docs). (I''m using ruby 1.9.2.) But it doesn''t, that is, it seems I am required to decode the json params explicitly. Why? Here''s an example of a post from my dev log... Processing by MarketlessPriceRequestsController#create as JSON Parameters: {"utf8"=>"✓", "price_request"=>"{\"prices_are_public_to_pricemakers\":false, ... Here''s params[:price_request] in the controller {"prices_are_public_to_pricemakers":false, ... But it looks like I need to decode the json explicity to get to the accepted form... {"prices_are_public_to_pricemakers"=>false, ... Can I configure my controllers to help me avoid the explicit decode step? Thanks, G -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/6e6d2e3d-6a6a-4de3-befd-c880fef68f2d%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung
2013-Jul-07 17:09 UTC
Re: I thought Rails 3.2 ActionController automatically converts JSON params hashes...
On Saturday, July 6, 2013 9:14:09 PM UTC+1, G-money wrote:> > Hey, > > I was thinking I''d have my controllers do double-duty: the same actions > handling json and html requests differentially. For this, it''s useful that > Rails 3.2 automatic params hash conversion from json work (as described > here<http://guides.rubyonrails.org/v3.2.13/action_controller_overview.html#json-xml-parameters>in the Rails docs). (I''m using ruby 1.9.2.) But it doesn''t, that is, it > seems I am required to decode the json params explicitly. Why? >Is the thing making the requests setting the content type to application/json ? Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/e80a03f0-6fe4-4f30-ab8e-0772aab353f9%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Matt Jones
2013-Jul-07 23:36 UTC
Re: I thought Rails 3.2 ActionController automatically converts JSON params hashes...
On Saturday, 6 July 2013 13:14:09 UTC-7, G-money wrote:> > Hey, > > I was thinking I''d have my controllers do double-duty: the same actions > handling json and html requests differentially. For this, it''s useful that > Rails 3.2 automatic params hash conversion from json work (as described > here<http://guides.rubyonrails.org/v3.2.13/action_controller_overview.html#json-xml-parameters>in the Rails docs). (I''m using ruby 1.9.2.) But it doesn''t, that is, it > seems I am required to decode the json params explicitly. Why? > > Here''s an example of a post from my dev log... > > Processing by MarketlessPriceRequestsController#create as JSON > Parameters: {"utf8"=>"✓", > "price_request"=>"{\"prices_are_public_to_pricemakers\":false, ... > > > Here''s params[:price_request] in the controller > > {"prices_are_public_to_pricemakers":false, ... > > > But it looks like I need to decode the json explicity to get to the > accepted form... > > {"prices_are_public_to_pricemakers"=>false, ... > > > Can I configure my controllers to help me avoid the explicit decode step? > >+1 to Fred''s suggestion: make sure the parameters are being *sent* as application/json. I''d also recommend double-checking the code that is sending the data - what you''re getting looks like what would result if you did something like this: (in Rails) render :json => { :price_request => @price_request.to_json } (in JS) $.post(''/some_url'', { price_request: JSON.generate(some_data_object) }) In both cases, there''s two conversions to JSON going on - one explicit, then the other implicit in the transmission (either the render or the post). --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4e7b11ad-1170-419b-be15-b8e42529f4d9%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.