Malformed params in rails controllers -


i writing action receive params hash this:

user = {:name=>"adam", :age=>15, :nickname=>"mynickname"}

should care if params correct hash? want know if need check if user hash , not string, integer or else.

i wrote nickname value

def nickname     if params[:user].present?         params[:user][:nickname].presence     end end 

but fails if params[:user] string , not hash. should write validations that? if so, best practices writing them?

you following :

nickname = params[:user][:nickname] rescue nil 

nickname nil if error occurs while browsing hash.

be careful rescue can hide errors, in case, it's pretty harmless since doing fetching values hash.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -