将其转换为Ruby 1.9哈希

时间:2021-10-23 22:30:32

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.

我将如何在新的1.9哈希语法中执行此操作?我尝试了很多不同的方法,但似乎无法理解语法。另外,我发现它相当令人困惑。

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

编辑:实际上已经意识到我已经将它与json:call混合在了一起。呸!讨厌两个人之间的差距。

2 个解决方案

#1


3  

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).

另外,在一行中将如此多的嵌套结构串在一起肯定会让人感到困惑。将其分解,使其可读(和可写)。

#2


0  

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

所以,除了一些微小的变化来绕过我无法访问的对象(@contact和@note),我改变的唯一的是

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

to

include: { contact: { except:

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

此外,您可能会考虑使用rabl进行此类操作。

#1


3  

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).

另外,在一行中将如此多的嵌套结构串在一起肯定会让人感到困惑。将其分解,使其可读(和可写)。

#2


0  

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

所以,除了一些微小的变化来绕过我无法访问的对象(@contact和@note),我改变的唯一的是

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

to

include: { contact: { except:

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

此外,您可能会考虑使用rabl进行此类操作。