gpt4 book ai didi

json - 当 format=json 时响应_with 重定向

转载 作者:行者123 更新时间:2023-12-01 02:50:05 25 4
gpt4 key购买 nike

我在我的 Controller 中遇到了一个奇怪的行为。他们似乎偶尔想要重定向而不是呈现 json 响应。

respond_to :json, :html, :js

def create
@favorite = current_user.favorites.build(:location_id=>params[:location_id])
if @favorite.save
respond_with(@favorite)
else
respond_with(@favorite.errors)
end
end

我认为它大部分时间都有效,但今天我收到了这个错误的通知:

NoMethodError: # 的未定义方法 `favorite_url'

params 哈希记录为:
{"format"=>"json",
"action"=>"create",
"user_id"=>"56",
"auth_token"=>"iGSty8CMIaWsbShYZEtw",
"location_id"=>"47943",
"controller"=>"favorites"}

特别奇怪,因为它似乎大部分时间都在工作......我已经改变了我的一些其他 Controller 来使用旧的 format.json { render :json => @object } 语法,但如果可能的话我想避免这种情况.

这怎么可能?

最佳答案

在不是 GET 的路径上,respond_with尝试重定向到任何给定的 url。您可以使用自定义响应程序覆盖它

关于json - 当 format=json 时响应_with 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5526328/

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