2013-03-22 6 views
5

का उपयोग कर मैं अपने कोड है, जब nested_form मणिअपरिभाषित विधि `values_at 'nested_form मणि

यहाँ का उपयोग कर त्रुटि

ActionView::Template::Error (undefined method `values_at' for nil:NilClass): 

हो रही है

मॉडल

class Poll < ActiveRecord::Base 

    attr_accessible :question, :poll_answers_attributes 
    has_many :poll_answers 
    accepts_nested_attributes_for :poll_answers 
end 

class PollAnswer < ActiveRecord::Base 
    belongs_to :poll 
    attr_accessible :answer 
end 

देखें

=nested_form_for [:admin, @poll], mutipart: true, class: "form-horizontal" do |f| 
    .span6 
     .control-group 
     =f.label :question, class: "control-label" 
     .controls 
      =f.text_field :question, rows: "5", class: "span5" 
     = f.link_to_add "Add a Answer", :poll_answers 
     =f.submit "Create Post", class: "btn btn-primary" 

StackTrace

/home/abid/.bundler/ruby/1.9.1/nested_form-6232dd853c27/lib/nested_form/builder_mixin.rb:41:in `block in link_to_add' 
/home/abid/.bundler/ruby/1.9.1/nested_form-6232dd853c27/lib/nested_form/view_helper.rb:53:in `call' 
/home/abid/.bundler/ruby/1.9.1/nested_form-6232dd853c27/lib/nested_form/view_helper.rb:53:in `after_nested_form_callbacks' 
/home/abid/.bundler/ruby/1.9.1/nested_form-6232dd853c27/lib/nested_form/view_helper.rb:8:in `block in nested_form_for' 

किसी भी विचार?

उत्तर

12

nested_form मणि के लिए दस्तावेज में उल्लेख किया है, आप link_to_load बटन से पहले field_for नेस्टेड वस्तु की उल्लेख करना होगा। मैंने पहले इस मणि का उपयोग नहीं किया है लेकिन दस्तावेज़ीकरण के माध्यम से जाने के बाद, मैं इसका अनुमान लगा रहा हूं।

यहाँ रूप की तरह

<%= nested_form_for [:admin, @poll], mutipart: true, class: "form-horizontal" do |f| %> 
    <%= f.text_field :question %> 
    <%= f.fields_for :poll_answers do |poll_ans_form| %> 
    <%= poll_ans_form.text_field :name %> 
    <%= poll_ans_form.link_to_remove "Remove this task" %> 
    <% end %> 
    <p><%= f.link_to_add "Add a Answer", :poll_answers %></p> 
<% end %> 
1

आपको इस प्रकार के नेस्टेड फॉर्म के लिए सरल_फॉर्म देखना चाहिए। यह बहुत आसान बनाता है। अपने मॉडल आप पोस्ट के आधार पर, कुछ इस तरह काम करना चाहिए:

# controller 
    def new 
    @poll = Poll.new 
    end 

# new.html.erb 
<%= simple_form_for @poll do |f| %> 
    <%= f.simple_fields_for :poll_answer do |l| %> 
    <%= l.input :answer, autofocus: true %> 
    <%= l.submit "Add", class: 'small round button' %> 
    <% end %> 
<% end %> 

फिर उस पद के लिए चुनाव # भेजा जाना चाहिए नियंत्रक बनाने के लिए यदि आप मार्ग खोला हुआ है। वहां से, आप इस पर अपना तर्क कर सकते हैं।

उम्मीद है कि मदद करता है।

+0

मैं कार्यावधि में जवाब जोड़ने की जरूरत है, वहाँ 3, 4, 5 हो सकता है तो thats जवाब के किसी भी संख्या लग रहा है कारण है कि मैं nested_form मणि उपयोग कर रहा हूँ – Abid

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