索引视图返回空值

时间:2012-10-10 15:36:16

标签: ruby-on-rails ruby ruby-on-rails-3 erb

我在控制器中有这个:

class TeamsController < ApplicationController
  respond_to :json 
  def index
    @teams = Teams.all
    respond_with @teams 
  end
  def show
    @team = Teams.find params[:id]
    respond_with @team
  end

这就是我的观点:

<%= render partial: "team", object: @team %> #file-show.json.erb
[<%= render partial: "team", collection: @teams, spacer_template: "comma" %>] #file-index.json.erb
<%= @team.to_json.html_safe %> #file- _team.json.erb

但teams.json的响应始终是[null,null,null],而teams / 1.json的响应是正确的{“id”...} 知道我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

_team.json.erb部分中,您使用的变量@team仅在您的show动作中设置。如果您使用team,那么它应该有效,因为部分呈现过程分别在show和index的情况下将其设置为@team或每个@teams

所以,你的部分应该是:

<%= team.to_json.html_safe %>
相关问题