Ruby on Rails - PUT Method Creates Extra Params Entry -
so observed weird behaviour while implementing endpoint restful api creating mobile client. using put method update attribute on user
model. send user's id url parameter , value update inside json object. seems work fine when check parameters via rails logs noticed strange. reason there parameter being sent backend can't seem explain. here logs seeing when call endpoint mobile client:
parameters: {"enable_security"=>true, "id"=>"7d7fec98-afba-4ca9-a102-d5d71e13f6ce", "user"=>{}}
as can seen above additional "user"=>{}
appended list of parameter entries. see when print out params object well. can't seem explain coming from. checked mobile client safe , there no in code send parameter key user
. puzzling me , makes me think missing simple. why there empty object user
key being sent backend restful api?
update provide more information
here code gets called when user hits endpoint updates user user
model:
#put /:id/user/update_security_settings def update_security_settings @user = user.find_by_id(params[:id]) @user.advanced_security_enabled = params[:enable_security] respond_to |format| if @user.save response = {:status => "200", :message => "user's security settings updated."} format.json { render json: response, status: :ok } else format.json { render json: @user.errors, status: :unprocessable_entity } end end end
update in response user's comments
here routes pertain user_controller
, view controller defines endpoints deal creating , updating user
model.
post '/user/upload_profile', to: 'user#upload_profile' '/:id/user', to: 'user#find_user' put '/:id/user/update_security_settings', to: 'user#update_security_settings' resources :user, :defaults => { :format => 'json' }
does comment mirror actual route?
#put /:id/user/update_security_settings
i'd expect /user/:id/update_security_settings
instead.
can show config/routes.rb - wild guess routes somehow configured expect actual nested user param, don't send (of course) , therefor appears empty in logs.
update: of routes unusual. don't need find_user route should covered under resources :user
show action (provided defined show method in controller, default way retrieve single resource item; no need find_user)
for custom routes update_security_settings action i'd suggest stick default pattern, resource/:id/action
and nesting in default resourceful route. putting id before resource unusual, confusing , may related issue (thoguh i#m not sure that). try cleaning routes.rb liek this:
# notice resources expects plural form :users resources :users member patch :update_security_settings post :upload_profile # other custom routes end end
this result in routes get /users
(index), get /users/1
(show) , patch /users/1/update_security_settings
.
more on routing found here: rails guides | routing outside in
please check if changes above remove empty user param.