D. Rakov
D. Rakov

Reputation: 338

Ruby on rails. Controller params pre-processing

I have a controller in ruby on rails 4 and i need to pre-process its params during request processing.

I receive parameters:

{"_json"=>[{"date"=>"9/15/2014", "name"="James"},{"date"=>"2/11/2014","name"=>"John"}]}

And i need to iterate through all json array elements and update name parameter by adding 'User' post fix. So, finally my json should be:

[{"date"=>"9/15/2014", "name"="James **User**"},{"date"=>"2/11/2014","name"=>"John **User**"}]

How can i do it in my controller?

Upvotes: 0

Views: 1258

Answers (1)

Vishal Jain
Vishal Jain

Reputation: 1940

You can try this way

params = {"_json"=>[{"date"=>"9/15/2014", "name"=>"James"},{"date"=>"2/11/2014","name"=>"John"}]}

Then modify params using using

params["json"].each { |h| h["name"] = "#{h['name']} **User**" }
puts params["_json"]

if you want to preprocess for each action then used before_filter

Upvotes: 1

Related Questions