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()。