0

Right now I have my index action set up like this, and I when the games component is rendered it basically receives games.to_json, I would like to reuse the template I have for GET /games.json which is specified in views/games/index.json.builder

I tried passing render(template: 'games/index.json') but I get this error:

Render and/or redirect were called multiple times in this action

def index
  @games = Game.all
  respond_to do |format|
    format.html { render component: 'games', props: { games: @games } }
    format.json { render :index }
  end
end

On views/games/index.json.jbuilder

json.array! @games do |game|
  json.extract! game, :id, :title, :cover_thumbnail
  json.url game_url(game, format: :json)
end

If I call GET /games.json I get something like this:

[
  {
    "id": 133,
    "title": "Final Fantasy VII: Remake",
    "coverThumbnail": "url",
    "url": "http://localhost:3000/games/133.json"
  },
  ...
]

But when I call GET /games, the games component receives every single attribute, I want to send only the attributes specified on the jbuilder template

Juan Fuentes
  • 1,743
  • 2
  • 20
  • 33
  • Did you place the code inside the format.json brackets? `format.json{ <----- place your rendering here ------> }` – BenKoshy Aug 19 '20 at 23:44
  • Yes, I tried `props: { games: render(template: 'games/index') }` but I get the double render error. The problem is rendering the html, not the json – Juan Fuentes Aug 19 '20 at 23:48

0 Answers0