I have this in my controller:
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
This is how my views look like:
<%= 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
but the response for teams.json is always [null, null, null] while the response for teams/1.json is correct {“id”…}
Any idea what am I doing wrong?
In your
_team.json.erbpartial you are using the variable@teamwhich is only set up in your show action. If you useteaminstead it should work because the partial rendering process sets that to be either@teamor each of@teamsin the case of show and index respectively.So, your partial should be :