0
I created my first API in Rails, and I’m using a simple model with to create a basic Crud, I managed to put my application to work I created the cute GET method returned 200, but in my POST is returning the following error in my DHC
I’m suspecting that I have to do some sort of validation to identify my decimal fields because this error appeared when I set the monetary fields {5.2} follows my migration file.
class CreateCnhs < ActiveRecord::Migration[5.1]
def change
create_table :cnhs do |t|
t.string :Descricao, :limit => 80
t.decimal :valorAula, precision: 5, scale: 2
t.decimal :ValorReteste, precision: 5, scale: 2
t.decimal :ValorAulaExtra, precision: 5, scale: 2
t.timestamps
end
end
error on line 15 of my control read where I get validation of my parameters, follow my controller.
module Api
module V1
class CnhController < ApplicationController
def index
cnhs = Cnh.order('created_at DESC');
render json: {status: 'SUCCESS', message: 'Solicitação atendida com sucesso!', Result:cnhs}, status: :ok
end
def show
cnhs = Cnh.find(params[:id])
render json: {status: 'SUCCESS', message: 'Loaded articles', data:cnhs}, status: :ok
end
def create
cnhs = Cnh.new(cnh_params)
if cnhs.save
render json: {status: 'SUCCESS', message: 'Saved articles', data:cnhs}, status: :ok
else
render json: {status: 'ERROR', message: 'Article not saved', data:article.errors}, status: :unprocessable_entity
end
end
def destroy
cnhs = Cnh.find(params[:id])
cnhs.destroy
render json: {status: 'SUCCESS', message: 'Deleted articles', data:cnhs}, status: :ok
end
def update
cnhs = Cnh.find(params[:id])
if cnhs.update_attributes(cnh_params)
render json: {status: 'SUCCESS', message: 'Updated articles', data:cnhs}, status: :ok
else
render json: {status: 'ERROR', message: 'Article not saved', data:article.errors}, status: :unprocessable_entity
end
end
private
def cnh_params
params.permit(:Descricao, :ValorAula, :ValorAulaExtra, :ValorReteste)
end
end
end
from what I understand he is trying to do some kind of validation with the decimal fields with limit in the houses {5,2}, because I did a test with a migration where I do not put limits of fields and works perfect my POST.
Show Thanks for the lesson.
– Thiago Ubiratan