Reputation: 592
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.
Upvotes: 1
Views: 770
Reputation: 31726
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.
Upvotes: 1
Reputation: 15772
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).
Upvotes: 4