How to get an array value from string in ruby on rails without eval?
Input params in controller will be
params[:city_id] = "1" or "[1,2,3,4,5]"
Expected results should be [1] or [1,2,3,4,5]
We can use eval(params[:city_id]).
Using eval method in controller is dangerous for sql.
Result:
params[:city_id].scan(/\d+/).map(&:to_i)
No comments:
Post a Comment