Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the nested JSON params from controller?

Here is my code in controller:

def login
  @user = params[:user]
  render :json => @user
end

And here is my POST data:

{
  "user": {
    "email":"2",
    "password":2
  }
}  

But I can only retrieve a JSON like:

{
  "email": "2",
  "password": 2
}

How can I parse JSON by params for both email and password, and use the parameter like

params[:email]
params[:password]

so that i can compare my params to the database. Thanks a lot!

like image 464
John Avatar asked May 19 '26 10:05

John


1 Answers

According to what I understood, you need to get the data email and password directly from params[]. This is simply a nested params so you can do

params['user']['email']
params['user']['password']
like image 141
Ojash Avatar answered May 21 '26 00:05

Ojash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!