Code Monkey home page Code Monkey logo

permitted_params's Introduction

permitted_params

The move from attr_accessible to Strong Parameters arguably improved security by increasing developers' awareness and visibility of which attributes were whitelisted for mass assignment. But once you get beyond toy applications, the standard practice of defining a foo_params method in each FooController leads to a lot of duplicated code, as you find yourself nesting the same structure in many different places.

The permitted_params gem addresses this problem by allowing you to specify the mass-assignment rules in a single location using a very simple DSL.

See more details on our blog: http://thesource.amitree.com/2014/02/protected-attributes.html.

This work was inspired by RailsCast #371. Thanks, ryanb!

Installation

Add to your Gemfile:

gem 'permitted_params'

Generate initializer:

rails generate permitted_params

Or create an initializer manually: config/initializers/permitted_params.rb:

PermittedParams.setup do |config|
  config.user do
    # We always permit username and password to be mass-assigned
    scalar :username, :password

    # email can be mass-assigned from create (but not from update)
    scalar :email if action_is(:create)

    # Only admins can change the is_admin flag.  Note that we can call
    # any controller methods (including current_user) from this scope.
    scalar :is_admin if current_user.admin?

    # We permit job_ids to be an array of scalar values
    array :job_ids

    # We permit person_attributes containing the whitelisted attributes
    # of person (see definition below)
    nested :person
  end

  config.person do
    # Inheritance!
    inherits :thing_with_name
  end

  config.thing_with_name do
    scalar :name
  end
end

Now in your controllers, you can simply write:

@user = User.create(permitted_params.user)

or:

user_attributes = { ... some hash ... }
@user = User.create(permitted_params.user(user_attributes))

permitted_params's People

Contributors

afn avatar nbwar avatar

Watchers

 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.