无法找到更新语句的原因

时间:2019-01-24 02:52:25

标签: ruby-on-rails

我能够从GET请求中检索记录,但是无法使用PATCH请求更新记录。关于如何调试此问题的任何建议?

def show
        @product = Product.find(params[:id])
    render json: { status: :ok, message: 'Find the product ', data: @product }
end

def update
        @product = Product.find(params[:id])

        if @product.update(product_params)
                render json: { status: :ok, message: 'Product updated!!! ', data: @product }
        else
            render json: { status: :error, message: 'Product not available!!!', data: @product }
    end
end

private

def product_params
        params.require(:product).permit(:title, :price, :count)
end

卷曲请求:

curl -i -X GET 'localhost:3000/products/1'  
      {"status":"ok","message":"Find the product ","data":{"id":1,"title":"cell phone","price":"999","count":"99","created_at":"2019-01-19T16:12:09.717Z","updated_at":"2019-01-21T09:01:56.056Z"}}
curl -i -X PATCH 'localhost:3000/products/1' -d '{"product":{ "title": "t", "price": "1.23", "count": "3"}}'
     {"status":"error","message":"Product not available!!!","data":{"id":1,"title":"t","price":"1.23","count":"3","created_at":"2019-01-19T16:12:09.717Z","updated_at":"2019-01-21T09:01:56.056Z"}}


              Prefix Verb   URI Pattern                                                                              Controller#Action
        welcome_index GET    /welcome/index(.:format)                                                                 welcome#index
     product_purchase GET    /products/:product_id/purchase(.:format)                                                 products#purchase
             products GET    /products(.:format)                                                                      products#index
                      POST   /products(.:format)                                                                      products#create
          new_product GET    /products/new(.:format)                                                                  products#new
         edit_product GET    /products/:id/edit(.:format)                                                             products#edit
              product GET    /products/:id(.:format)                                                                  products#show
                      PATCH  /products/:id(.:format)                                                                  products#update
                      PUT    /products/:id(.:format)                                                                  products#update
                      DELETE /products/:id(.:format)                                                                  products#destroy
                 root GET    /     

1 个答案:

答案 0 :(得分:0)

您正在GET中使用product,在PATCH请求中使用products。检查路由文件是否定义了正确的路由。

还根据您的卷曲响应,似乎产品已更新。但是状态和消息来自else块。尝试使用update!检查是否有错误