我有SettingsController
它允许我管理User
的设置。
class Api::V1::SettingsController < Api::V1::BaseController
def show
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
@user.settings = JSON.parse(params[:settings])
authorize! :edit, @user
if @user.save
render status: :ok
else
render status: :bad_request
end
end
end
和路线
resources :settings, only: [:show, :update]
Settings 是一个文本字段,其中包含:serialize :settings, HashWithIndifferentAccess
所以我用参数发送PUT
请求http://mypage.com/api/vi/settings/:id.json
:"{\"settings\":{\"color\":\"red\"}}"
但我得到:
uninitialized constant Setting
{"_json"=>"{\"settings\":{\"color\":\"red\"}}",
"id"=>"1",
"format"=>"json",
"setting"=>{"_json"=>"{\"settings\":{\"color\":\"red\"}}"}}
在这种情况下我应该如何设置路线?
我的路线:
app(development)» rake routes |grep settings
api_v1_setting GET /api/v1/settings/:id(.:format) api/v1/settings#show
PATCH /api/v1/settings/:id(.:format) api/v1/settings#update PUT /api/v1/settings/:id(.:format) api/v1/settings#update
当我使用非资源路由时:
put 'settings/:id' => 'settings#update'
app(development)» rake routes |grep settings
api_v1 PUT /api/v1/settings/:id(.:format) api/v1/settings#update
我犯了同样的错误。