working with decimal {5,2} Rails

Asked

Viewed 577 times

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

inserir a descrição da imagem aqui

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.

1 answer

2


The problem has nothing to do with decimals, but with field nomenclature.

Your model does not contemplate ValorAula, and yes, as shown in migration file, valorAula.

The ActionController::Parameters#permit is case-sensitive, so when making the Strong Parameters, use:

params.permit(:Descricao, :valorAula, :ValorAulaExtra, :ValorReteste)

Another thing, in Rails it is common to adopt as convention the nomenclature of columns using Snake case, this way:

descricao
valor_aula
valor_aula_extra
valor_reteste

Consider changing the capitalization, avoid this kind of error you got.

Following the Rails conventions, you will be productive. This is due to the Convention over Configuration.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.