Coder Social home page Coder Social logo

yihangho / require_params Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 26 KB

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

Home Page: https://rubygems.org/gems/require_params

License: MIT License

Ruby 76.99% JavaScript 2.73% CSS 2.80% HTML 17.48%

require_params's Introduction

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.

require_params's People

Contributors

yihangho avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.