-1

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

同じエラーが発生します。

4

2 に答える 2

-2

設定モデルがないため、この場合はリソースフルを使用できません。非リソース ルーティングを使用する必要があります。

http://guides.rubyonrails.org/routing.html#non-resourceful-routes

于 2013-08-09T10:01:13.017 に答える