2

我有两个模型用户和食谱。我想获取配方页面上的用户字段和用户页面上每个配方中的用户名。我不知道该怎么做。在 Rails 中,我有两个在用户和配方之间有很多关联。我添加了从 rails 获得的 json 数组的示例。

我的 json 用户数组

user: {
  id: 1,
  name: "ejiqpep",
  email: "ejiqpep@gmail.com",
  recipe_ids: [
    1,
    2
  ],
  box_recipe_ids: [
    1,
    2
  ]
}

我的 json 配方数组

recipe: {
  id: 1,
  title: "recipe01",
  about: "about01",
  ingredients: "ingredients01",
  steps: "steps01",
  visibility_type: true,
  slug: "recipe01",
  user: {
    id: 1,
    name: "ejiqpep",
    email: "ejiqpep@gmail.com",
    recipe_ids: [
      1,
      2
    ],
    box_recipe_ids: [
      1,
      2
    ]
  }
}

用户.js.coffee

App.User = DS.Model.extend
  name: DS.attr('string')
  email: DS.attr('string')
  recipes: DS.hasMany('App.Recipe')
  box_recipes: DS.hasMany('App.Recipe')

recipe.js.coffee

App.Recipe = DS.Model.extend
  user: DS.belongsTo('App.User')
  title: DS.attr('string')

user.handlebars (我想在每个配方中获取用户名)

<h1>{{name}}</h1>
<p><b>Email:</b> {{email}}</p>

<h3>User recipes</h3>
{{#each recipe in recipes}}
  <div>
    <span>{{recipe.id}}</span>
    <span>!!!I want to show username here!!!</span>
    <span>{{#linkTo recipe recipe}} {{recipe.title}} {{/linkTo}}</span>
  </div>
{{/each}}

<h3>User box_recipes</h3>
{{#each fav in box_recipes}}
  <div>
    <span>{{fav.id}}</span>
    <span>!!!I want to show username here!!!</span>
    <span>{{#linkTo recipe fav}} {{fav.title}} {{/linkTo}}</span>
  </div>
{{/each}}

recipe.handlebars

<h1>{{title}}</h1>
<p>User {{user.name}}</p>  # I want to show username here!!!
4

1 回答 1

0

我不知道它是否有效......但试一试:

更新:

{{#each recipes}}
  <div>
    <span>{{id}}</span>
    <span>{{../name}}!!!I want to show username here!!!</span>
    <span>{{#linkTo recipe this}} {{title}} {{/linkTo}}</span>
  </div>
{{/each}}
于 2013-04-13T19:48:46.497 回答