Skip to content

underscorize_params, a ruby Gem that allows you to send parameters in camelcase to your rails api, without worrying about to transform them in snakecase

License

Notifications You must be signed in to change notification settings

monade/underscorize_params

Repository files navigation

underscorize_params

Installation

Add the Gem into your Gemfile

gem 'underscorize_params', github: 'monade/underscorize_params'

This gem allows you to send params to a Controller's action in camel case. It gets included inside ActionController::Base

ActionController::Base.include UnderscorizeParams::Controller if defined?(ActionController::Base)

Suppose you have these Controller and Model:

models/user.rb

class User < ActiveRecord::Base
  attribute :first_name, :String
  attribute :last_name, :String
end

controllers/users_controller.rb

class UsersController < ActionController::Base
  def create
    @user = User.new create_params

    if @user.save
      render json: @user
    else
      render json: @user.errors
    end
  end

  private

  def create_params
    params.permit(:first_name, :last_name)
  end
end

this gem allows you to call this controller's api with parameters in camel case, such as:

const params = {
  firstName: 'Friedrich',
  lastName: 'Nietzsche'
}

axios.post('/users', params)

the gem's job is very simple, received parameters get transformed into snake case in the before_action hook. Check out the code to find out, is very simple!

License

The gem is available as open source under the terms of the MIT License.

About Monade

monade

underscorize_params is maintained by mònade srl.

We <3 open source software. Contact us for your next project!

About

underscorize_params, a ruby Gem that allows you to send parameters in camelcase to your rails api, without worrying about to transform them in snakecase

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages