2011-03-02 51 views
0

请参考代码。如何将text_fields映射到Rails 3中的哈希对象字段?

<%= 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 %> 

这里, 'json_parsed' 那就是我曾json_decode后得到了哈希对象。 first_name/middle_name/etc。是该散列对象中的所有字段。现在我想要在text_field中获取这些值。但它给错误“未定义的方法”first_name'为散列“。

我怎样才能显示这些值散列直接到text_field?

回答

1

您不能使用text_field作为散列对象。它可以用于模型对象(具有您调用的名称的方法的对象,例如,@json_parsed应该有first_name的方法,以便我们可以调用@json_parsed.first_name)。对于哈希,我们不能这样称呼它。所以,你应该要么使用text_field_tag这样

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

还是应该散列转换成Ruby类对象,具有相应的方法名称,使用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 

,并用它来作对象,

@json_parsed = Hashit.new(json_parsed_hash) 

,并用它在意见,你只是做。有关Hashit的更多详细信息,请参阅此link

您的联系哈希值,你应该使用这样

<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> 

也许,你应该在意见

<% 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

看到我的哈希对象这样使用它包含: {“FIRST_NAME” =>“Aashish”,“middle_name”=>“Shrikant”,“last_name”=>“Pathak”,“contact”=> {“email”=>“[email protected]”,“phone_no”=>“9922582272 “},”profile“=> {age} =>”22“,”ht“=>”175“,”wt“=>”67“}} 我试过像你说的那样将它转换为类对象。但它给错误'未定义的方法[] Hashit' – Aashish 2011-03-02 09:15:22

+0

如果你看到的哈希对象,它是嵌套的。基本上这是我从一个web服务获得的。现在我想发送同样的结构回其他一些web服务来保存新数据。 – Aashish 2011-03-02 09:18:38

+0

你应该编辑你的'%@contact = @json_parsed [“contact”]%>'line to'<%@contact = @ json_parsed.contact]%>'并且使用'fields_for'进行联系。我会编辑我的答案。 – rubyprince 2011-03-02 09:22:20