Get value of a specific param - Rails

2019-07-06 20:55发布

问题:

In a specific controller I have the below list of params:

Parameters: {"user"=>"{\"id\":32,\"email\":\"test@test.com\",\"created_at\":\"2014-04-10T13:13:40.000Z\",\"updated_at\":\"2014-04-11T18:10:15.000Z\"}"}

How I can get the value of email for example?

回答1:

You value looks like json.

So try this

user_params = ActiveSupport::JSON.decode(params[:user])
user_params[:email]

or

require 'json'
user_params = JSON.parse(params[:user])
user_params[:email]


回答2:

You can do

params[:user][:email] #=> "test@test.com"

Which gives you the params of email attribute of user.