2011-07-29 22 views
5

मेरे पास एक नेस्टेड मॉडल संरचना है जो इस तरह दिखती है:नेस्टेड फॉर्म दिखाई नहीं दे रहा है!

resources :users, :path => '/' do 
    resources :accounts do 
     resources :characters 
    end 
end 

मैं accounts#new पेज को दोनों रूपों को दिखाने के लिए प्राप्त करने की कोशिश कर रहा हूं लेकिन किसी कारण से केवल खाता प्रपत्र प्रदर्शित होता है (screenshot)। https://github.com/imjp/d2shed

account.rb

class Account < ActiveRecord::Base 
attr_accessible :account_name, :realm 
accepts_nested_attributes_for :characters 

belongs_to :user 
has_many :characters, :dependent => :destroy 

validates :account_name, :presence => 'true', 
        :length => { :in => 4..20 }, 
        :uniqueness => 'true' 

validates_presence_of :realm 
validates_format_of :account_name, :with => /^[A-Za-z\d_]+$/ 
end 



accounts_controller.rb

def new 
    @user = User.find(params[:user_id]) 
    @account = Account.new 
    @account.characters.build 
end 



_form:

यहाँ Git है। html.erb

<%= form_for([@user, @account]) do |f| %> 
<div class="field"> 
<%= f.label :account_name %><br /> 
<%= f.text_field :account_name %> 
</div> 
<div class="field"> 
<%= f.radio_button(:realm, "USWest") %> 
    <%= f.label(:realm, "USWest") %> 

    <%= f.radio_button(:realm, "USEast") %> 
    <%= f.label(:realm, "USEast") %> 

    <%= f.radio_button(:realm, "Europe") %> 
    <%= f.label(:realm, "Europe") %> 

    <%= f.radio_button(:realm, "Asia") %> 
    <%= f.label(:realm, "Asia") %> 
</div>  

<%= f.fields_for :character do |character_form| %> 

     <div class="field"> 
      Name: <%= character_form.text_field :name %> 
     </div> 
    <% end %> 

<div class="actions"> 
<%= f.submit %> 
</div> 
<% end %> 

संपादित करें: कोड अब काम करता है!

उत्तर

7

आप fields_for ".." क्यों कर रहे हैं?

आप क्या करना चाहते हैं <%= f.fields_for :characters %> है क्योंकि यह सभी पात्रों के माध्यम से फिर से शुरू हो जाएगा और खाते के रूप में आवश्यक फ़ील्ड प्रस्तुत करेगा। f ऑब्जेक्ट पर fields_for पर कॉल करके आप मूल रूप से बता रहे हैं कि इसमें नेस्टेड विशेषताएँ हैं।

दूसरा, आपको @user.accounts.build के साथ किसी अन्य को बनाने के बजाय @account ऑब्जेक्ट का उपयोग अपने फॉर्म में करना होगा। इसे गलत तरीके से करके, आप वास्तव में एक नया खाता ऑब्जेक्ट बना रहे हैं जिसमें इसके लिए पूर्व-निर्मित कोई भी वर्ण वस्तु नहीं होगी।

इसके अतिरिक्त आपको accepts_nested_attributes_for :characters को अपने Account मॉडल में निर्दिष्ट करने की आवश्यकता होगी ताकि फ़ॉर्म उन्हें खाता पैरामीटर के साथ स्वीकार कर सके।

+0

उत्तर देने के लिए धन्यवाद! मैंने कोड को संशोधित किया लेकिन यह अभी भी काम नहीं कर रहा है:/मैंने कोड को अपडेट करने के लिए अपना प्रश्न संपादित कर लिया है। क्या आप देख सकते हैं कि और क्या गुम हो सकता है? – imjp

+0

@imjp: यदि आपने स्पष्ट किया है कि "अभी भी काम नहीं कर रहा है" तो शायद हां। –

+0

@imjp: असल में मैंने देखा कि आपने अपने फॉर्म में गलती की है। मेरे उत्तर में मध्य अनुच्छेद उस गलती को हल करता है। –

संबंधित मुद्दे