asp.net web api - Format of params structure expected by Rails 5 -
i'm working on api-only rails 5 app -- yes, know. haven't been around rails while, doing long overdue catching when bumped ol' nested hash of parameters, had forgotten about. want create user
, thought i'd issue post request following parameters (let's make json simplicity):
{ "username": "myusername", "password": "mypassword" }
however, after grieving , googling, discovered/remembered i'm supposed pass following:
{ "user": { "username": "myusername", "password": "mypassword" } }
which in turn makes following
def user_params params.request(:user).permit(:username, :password) end
happy.
all in all, i'm ok that. going private api mobile app, , don't expect open anyone. however, i've been perusing other (typically restful-ish-like) apis around web, , none of them have nested model
business going on. i'll working on public api different project, wondering if there particular reason requiring structure, and, if not, how can maintain strong parameters without nesting.
it's convenient params[:user]
refer parameters user , without risk of clashes magic parameters such controller, action, format , on.
you can use parameter wrapping make easy api clients
if add
wrap_parameters format: [:json]
to controller json requests rails wrap parameters match user attributes in hash (if controller isn't called userscontroller
you'll need tell action controller class use or parameters should wrapped). allows controller code stay unchanged api clients post "bare" parameters.
you can enable globally whole application.
Comments
Post a Comment