working with decimal {5,2} rails

0

I created my first API in Rails, and I'm using a simple template with to create a basic Crud, I managed to put my application to work I created the GET method cute little returned 200, only that in my POST it is returning the following error in my DHC

I'm suspecting that I have to do some sort of validation for it 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

The error in line 15 of my control read where I get the 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 

By what I realize it is trying to do some sort of validation with the decimal fields with boundary in the houses {5.2}, because I did a test with a migration where I do not place limitation of fields and works my POST perfectly.

    
asked by anonymous 29.01.2018 / 03:45

1 answer

2

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

Your model does not include ValorAula , but rather, as shown in the migration file, valorAula .

The ActionController::Parameters#permit is case-sensitive, so when doing the strong parameters, use:

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

Another thing, in Rails it is common to adopt the naming convention for columns using snake case, as follows:

descricao
valor_aula
valor_aula_extra
valor_reteste

Consider changing capitalization, you'd avoid this kind of mistake you got.

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

    
29.01.2018 / 11:24