Accessing the child instance in a RABL template

后端 未结 4 2131
不思量自难忘°
不思量自难忘° 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:36

    Another approach to keep things DRY:

    contacts/show.json.rabl

    object @contact
    node do |contact|
        if contact.is_foo?
            {:a1 => contact.a1, :a2 => contact.a2}
        else
            {:a3 => contact.a3, :a4 => contact.a4}
        end
    end
    

    users/show.json.rabl

    object @user
    attributes :name
    child :contacts do
        extends 'contacts/show'
    end
    

提交回复
热议问题