Get value of a specific param - Rails

白昼怎懂夜的黑 提交于 2019-12-10 16:51:23

问题


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.



来源:https://stackoverflow.com/questions/23020928/get-value-of-a-specific-param-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!