Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4034308
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:57:21+00:00 2026-05-20T11:57:21+00:00

Please refer to code. <%= form_tag(:action => create_user, :method => post) do%> <p><label for=first_name>First

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:57:22+00:00Added an answer on May 20, 2026 at 11:57 am

    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_parsed should 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 this

    <%=text_field_tag 'json_parsed[first_name]', :value => @json_parsed["first_name"]%> 
    

    or you should convert the hash into ruby class object with the corresponding method names, using Hashit.

    class Hashit
      def initialize(hash)
        hash.each do |k,v|
          v = Hashit.new(v) if v.is_a?(Hash)
          self.instance_variable_set("@#{k}", v) ## create and initialize an instance variable for this key/value pair
          self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")}) ## create the getter that returns the instance variable
          self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)}) ## create the setter that sets the instance variable
        end
      end
    end
    

    and use it to make the object,

    @json_parsed = Hashit.new(json_parsed_hash)
    

    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

    <p><label for="last_name">Email</label>:
    <%= fields_for @json_parsed.contact do |p|%
        <%= p.text_field 'email'%></p>
    <p><label for="last_name">Phone</label>:
        <%= p.text_field 'phone_no') %></p>
    

    Perhaps, you should use it like this in the views

    <% form_for :json_parsed, :url => {:action => "create_user"} do |f| %>
    <p><label for="first_name">First Name</label>:
        <%= f.text_field 'first_name' %></p>
    <p><label for="middle_name">Middle Name</label>:
        <%= f.text_field 'middle_name' %></p>
    <p><label for="last_name">Last Name</label>:
        <%= f.text_field 'last_name' %></p>
        <% f.fields_for 'contact' do |p| %>
    <p><label for="last_name">Email</label>:
        <%= p.text_field 'email' %></p>
    <p><label for="last_name">Phone</label>:
        <%= p.text_field 'phone_no' %></p>
    <% end %>
    <%= submit_tag "Create" %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please refer the below code. I want the boxes for Blue and all other
Please refer to the code below, when I comment in either of the commented
Hi please refer the following HTML code: <div id=content> <p> <font size='2'> <img src=something.jpg
HI all can anyone please refer me, the iphone image gallery source code? Any
How can I enumerate through an IDictionary? Please refer to the code below. public
Please, provide code examples in a language of your choice. Update : No constraints
Im new to C# programming. Can someone please explain the following code: Console.WriteLine( {0}{1,10},
Please suggest some good resources to start writing Java Web services.
Please read the whole question. I'm not looking for an approach to managing multi-lingual
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.