rubysinatrapadrinojbuilder

How can I separate templates by data in jbuilder


I want to make following data with jbuilder.
How can I make jbuilder separated?

data: [{
  type: "top"
  logo: "logo.png"
  title: "title"
  },{
  type: "nav"
  background: "bg.png"
  content: "<div> Welcome </div>"
  },{
  type: "footer"
  content: "Copyright: xx"
  }
]

I did it like that, but couldn't running

data.jbuilder

json.set! :result do
  json.array! data do |item|
     case item.type
     when 'top'
       json.partial! '_top', item: item
     when 'nav'
       json.partial! '_nav', item: item
     when 'footer'
       json.partial! '_footer', item: item
     else 
       return nil
  end
end

_top.jbuilder

json.logo   item.logo
json.title  item.title

_nav.jbuilder

json.background item.background
json.content    item.content

_footer.jbuilder

json.content item.content

How can I solve the problem?


Solution

  • when you call partials, _ is not needed you just give the path if it is in different folder or name if in same folder.

    try

    json.partial! 'top', item: item
    json.partial! 'nav', item: item
    json.partial! 'footer', item: item