2011-03-02 47 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