我在控制器中遇到了一个奇怪的行为。他们似乎偶尔想要重定向而不是呈现 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:#<FavoritesController:0x00000006171dc0> 的未定义方法 `favorite_url'
参数哈希记录为:
{"format"=>"json",
"action"=>"create",
"user_id"=>"56",
"auth_token"=>"iGSty8CMIaWsbShYZEtw",
"location_id"=>"47943",
"controller"=>"favorites"}
特别奇怪,因为它似乎大部分时间都在工作......我已经更改了一些其他控制器以使用旧的 format.json { render :json => @object } 语法,但如果可能的话我想避免这种情况.
这怎么可能?