gpt4 book ai didi

ruby-on-rails - Rails 3.1 中 `respond_with` 和 `respond_to` 的异常行为

转载 作者:太空宇宙 更新时间:2023-11-03 16:10:00 27 4
gpt4 key购买 nike

我正在尝试让我的行为响应 :html:json .

class GamesController < ApplicationController

respond_to :html, :json

def index
@games = current_user.games
respond_with(@games)
end

end

我转到 /games 时的预期 View 就是看views/games/index.haml的布局如果我去/games.json我应该看到 @games作为浏览器中的 JSON 数据。

但我所拥有的是,当我去 /games.json我看到了 /games 的源代码其布局等为 Json( header 中带有 application/json 的 HTML 代码”

最佳答案

您必须通过文件的扩展名来告诉 Rails 我是否要渲染它!

views/games/index.haml 应该是 views/games/index.html.haml 我刚刚用我制作的一个使用 erb 的应用程序检查了这个并且它工作正常......你是否尝试过旧版本 rails ? - 来自 Gazler

关于ruby-on-rails - Rails 3.1 中 `respond_with` 和 `respond_to` 的异常行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5940059/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com