0

我已經創建了一個嵌套的模型窗體,用於向問題和解答發佈課程。它沒有拿起咖啡腳本,所以我已經添加了咖啡代碼作爲Javascript,但我得到一個錯誤和添加問題不起作用。任何人,請協助。Rails 3使用Javascript嵌套模型窗體

Lesson.rb

has_many :questions 
accepts_nested_attributes_for :questions, :allow_destroy => true 

Question.rb

belongs_to :lesson 
has_many :answers 
accepts_nested_attributes_for :answers, :allow_destroy => true 

Answer.rb

belongs_to :question 

application_helper.rb

def link_to_add_fields(name, f, association) new_object = f.object.send(association).klass.new 
    id = new_object.object_id 
    fields = f.fields_for(association, new_object, :child_index => id) do |builder| 
    render(association.to_s.singularize + "_fields", :f => builder) 
end 
link_to(name, '#', :class => "add_fields", :data => {:id => id, :fields => fields.gsub("\n", "")}) end 

管理/經驗/ _form.html.erb

<%= form_for([:admin,@lesson]) do |f| %> 
      <% if @lesson.errors.any? %> 
      <div class="notification error png_bg"> 
       <a href="#" class="close"><img src="/assets/admin/icons/cross_grey_small.png" title="Close this notification" alt="close" /></a> 
       <div> 
       <h2><%= pluralize(@lesson.errors.count, "error") %></h2> 

        <ul> 
        <% @lesson.errors.full_messages.each do |msg| %> 
        <li><%= msg %></li> 
        <% end %> 
        </ul> 
       </div>       
      </div> 
      <% end %> 

    <label class="formlabel">Lesson Title</label> 
    <%= f.text_field :title %> 

<%= f.fields_for :questions do |builder| %> 
<%= render 'question_fields', :f => builder %> 
<% end %> 
<%= link_to_add_fields "Add Question", f, :questions %> 

<%= f.submit 'Save', :class => 'button' %> 

_question_fields.html.erb

<fieldset> 
    <%= f.label :content, "Question" %><br /> 
    <%= f.text_area :content %><br /> 
    <%= f.check_box :_destroy %> 
    <%= f.label :_destroy, "Remove Question" %> 
    <%= f.fields_for :answers do |builder| %> 
     <%= render 'answer_fields', :f => builder %> 
    <% end %> 
    <%= link_to_add_fields "Add Answer", f, :answers %> 
</fieldset> 

_answer_fields.html.erb

<fieldset> 
    <%= f.label :content, "Answer" %> 
    <%= f.text_field :content %> 
    <%= f.check_box :_destroy %> 
    <%= f.label :_destroy, "Remove Answer" %> 
</fieldset> 

這裏是JavaScript我剛纔添加頭標籤結束之前。我得到的「$錯誤( '形式')上是不是一個函數。(在 '$(' 形式 ')' 中, '$(' 形式」 ......

<script> 
      jQuery(function() { 
       $('form').on('click', '.remove_fields', function(event) { 
       $(this).prev('input[type=hidden]').val('1'); 
       $(this).closest('fieldset').hide(); 
       return event.preventDefault(); 
       }); 
       return $('form').on('click', '.add_fields', function(event) { 
       var regexp, time; 
       time = new Date().getTime(); 
       regexp = new RegExp($(this).data('id'), 'g'); 
       $(this).before($(this).data('fields').replace(regexp, time)); 
       return event.preventDefault(); 
       }); 
      }); 
     </script> 
+0

什麼是你的jQuery版本? – Joeyjoejoe

+0

jQuery JavaScript庫v1.4.3 – Clay

+1

你需要如果你不想升級jquery,你可以使用.bind代替.on, – Joeyjoejoe

回答

0

jQuery的.on()函數至少需要jQuery 1.7的工作。更新你的jQuery或使用.bind()代替.on()。