Please refer to code.
<%= form_tag(:action => "create_user", :method => "post") do%>
<p><label for="first_name">First Name</label>:
<%= text_field 'json_parsed', 'first_name') %></p>
<p><label for="middle_name">Middle Name</label>:
<%= text_field 'json_parsed', 'middle_name') %></p>
<p><label for="last_name">Last Name</label>:
<%= text_field 'json_parsed', 'last_name') %></p>
<% @contact = @json_parsed["contact"] %>
<p><label for="last_name">Email</label>:
<%= text_field 'contact','email']) %></p>
<p><label for="last_name">Phone</label>:
<%= text_field 'contact', 'phone_no') %></p>
<%= submit_tag "Create" %>
<% end %>
here, ‘json_parsed‘ is the hash object which i have got after json_decode. first_name/middle_name/etc. are all fields in that hash object. Now i want to get those values in text_field. But it is giving error “undefined method ‘first_name‘ for hash”.
How can i show those values in hash directly into text_field?
You cannot use text_field for hash objects. It can be used for model objects(objects which have a method with the name you call eg.
@json_parsedshould have a method first_name such that we can call@json_parsed.first_name). For hash, we cannot call it like that. So, you should either use text_field_tag like thisor you should convert the hash into ruby class object with the corresponding method names, using Hashit.
and use it to make the object,
and use it in the views as you just did. For more details on Hashit, refer this link.
For your contact hash, you should use it like this
Perhaps, you should use it like this in the views