简体   繁体   中英

Convert this to Ruby 1.9 Hash

I have the following in the old syntax:

render json: [@note.to_json(:include => { :contact => { :except => [:created_at, :updated_at]}}, only: :body)], status: :created, location: [@contact, @note]

How would I do this in the new 1.9 hash syntax? I've tried plenty of different ways but can't seem to understand the syntax. On a side note, I find it rather confusing.

Edit: Actually realized I'm already mixing it in with the json: call. Bah! Hate going between to two.

I'm not sure where the trouble is, but it converts pretty simply to this:

render json: [@note.to_json(include: { contact: { except: [:created_at, :updated_at]}}, only: :body)], status: :created, location: [@contact, @note]

As an aside, stringing so many nested structures together on one line is bound to be confusing. Break it down so it's readable (and writable).

Your example works fine. Only need to change a few more keys

require 'pp'
pp json: [
    {
      include: { 
        contact: { 
          except: [:created_at, :updated_at]
        }
      },
      only: :body
    }
  ], 
  status: :created, 
  location: %w[contact note]

So, aside from some slight changes to get around objects I don't have access to (@contact and @note) the only ones I changed were

:include => { :contact => { :except =>

to

include: { contact: { except:

Also, you might look into using rabl for this sort of thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM