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]
設定は、次のテキスト フィールドです。serialize :settings, HashWithIndifferentAccess
だから私はparamsで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
同じエラーが発生します。