【问题标题】:How can I separate templates by data in jbuilder如何在 jbuilder 中按数据分隔模板
【发布时间】:2017-06-08 13:27:34
【问题描述】:

我想用 jbuilder 制作以下数据。
如何让 jbuilder 分离?

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

我是这样做的,但无法运行

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

我该如何解决这个问题?

【问题讨论】:

    标签: ruby sinatra padrino jbuilder


    【解决方案1】:

    当您调用partials时,不需要_,如果它在不同的文件夹中,您只需提供路径,如果在同一文件夹中,则提供名称。

    试试

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 2019-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      相关资源
      最近更新 更多