Accessing the child instance in a RABL template

后端 未结 4 2132
不思量自难忘°
不思量自难忘° 2021-01-06 07:26

I have a RABL template as shown below

object @user
attributes :name
child :contacts do
  # does not work
  if contact.is_foo?
    attributes :a1, :a2
  else
         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-06 07:49

    I know it's a late reply but came across the similar problem so thought of answering.

    It's more like a hack but works.

    When two variables are used as block argument contact and a random variable x, contact refers to an object of the collection

    When one variable is used in block argument, it renders collection object

    object @user
    attributes :name
    child :contacts do |contact, x|
      if contact.is_foo?
        attributes :a1, :a2
      else
        attributes :a3, :a4
      end
    end
    

提交回复
热议问题