How to map the text_fields to Hash objects fields in Rails 3?
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_pa开发者_如何转开发rsed', '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_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 %>
精彩评论