Last active
December 15, 2015 02:28
-
-
Save mikz/5186954 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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