Chinu Chinu - 5 months ago 3x
Ruby Question

Ruby - How to get assotiative hash value in view?

I am new in ruby. Now I am doing one small project. But I am unable to getting user data from hash values. Here is my coding -

@user = {'name'=>"Chinmay", "email"=>"", "phone"=>"1234567890", "age"=>30}

Above array I have tried to get each in

Here is code -

Name : <%= %>
Email: <%= %>
Phone: <%= %>
Age: <%= @user.age %> Years

Here I am getting error message :

undefined method `name' for #<Hash:0x5e040d0>

Please help me. Let me know how to get hash value without using any loop?

Again I was trying
that one also not working. blank message coming.

Thank you for answering.


First thing, your @user is not an array, it's a hash

Basically a hash is a key-value pair and in your input, name is a key and Chinmay is a value and so on!

Each named key is a symbol with which you can access the value in hash

So, <%= @user['name'] %> is the way you use if you want to access the name from your hash.

This will do your job!

Name : <%= @user['name'] %>
Email: <%= @user['email'] %>
Phone: <%= @user['phone'] %>
Age:   <%= @user['age'] %> Years