Skip to content

Instantly share code, notes, and snippets.

@mikz
Last active December 15, 2015 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikz/5186954 to your computer and use it in GitHub Desktop.
Save mikz/5186954 to your computer and use it in GitHub Desktop.
# flat api params like: name=John&surnname=Doe
POST '/api/user', name: 'John', surname: 'Doe'
# nested api params like: user[name]=Jack&user[surname]=Xzbit
POST '/api/user', user: { name: 'Jack', surname: 'Xzbit' }
# how to get only params that are related to the model?
# imagine case:
POST '/api/accounts/14/user', name: 'New', surname: 'User'
params == { account_id: 14, name: 'New', surname: 'User' }
# we really don't want to assign the account_id to the new user
# wrap_parameters to the rescue!
wrap_parameters User # if User is real model and has some columns
wrap_parameters :user, include: [:name, :surname] # if we don't have proper model
#and then in the controller access it as:
params[:user] == { name: 'New', surname: 'User' }
# so when someone passes flat params, the ones set up to be wrapped (name, surname) will be wrapped to the :user param
# when someone passes nested params, the params[:user] will already include name and surname and nothing will happend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment