0.0
No commit activity in last 3 years
No release in over 3 years
require_params ensures that parameters that your Rails API actions require are present.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

< 5.1, >= 5.0.0.beta2
 Project Readme

require_params

require_params ensures that parameters that your API actions require are present.

Usage

Include RequireParams to your ApplicationController:

class ApplicationController < ActionController::API
  include RequireParams
end

Use the require_params class method in your controllers to specify which parameters are required for which actions:

class UsersController < ApplicationController
  require_params [:username, :email, :password], only: :create

  def create
    # Do important stuff here
  end
end

require_params can be called multiple times for each actions:

class UsersController < ApplicationController
  require_params [:username, :password]
  require_params [:email], only: :create

  # Equivalent to calling
  # require_params [:username, :password, :email], only: :create
  # require_params [:username, :password], except: :create

  def create
    # Make a new user
  end

  def sign_in
    # Signs in user
  end
end

If any one of the required parameters is missing, your action handler will not be called at all. Instead, an error hash that looks like this will be served with 400 Bad Request:

{
  errors: {
    username: ["is missing"],
    password: ["is missing"]
  }
}

Internals

require_params is backed by prepend_before_action. As a result, it can be guaranteed that by the time your before_action hooks and actions are executed, the required parameters are present.

Installation

Add this line to your application's Gemfile:

gem 'require_params'

And then execute:

$ bundle

Or install it yourself as:

$ gem install require_params

Include RequireParams to your controllers:

class ApplicationController < ActionController::API
  include RequireParams
end

License

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