【问题标题】:Rails 3 Nested Model Form with Javascript带有 Javascript 的 Rails 3 嵌套模型表单
【发布时间】:2016-06-21 09:40:19
【问题描述】:

我为“问答课程”创建了一个嵌套模型表单。它没有拿起咖啡脚本,所以我将咖啡代码添加为 Javascript,但我收到错误并且添加问题不起作用。请任何人帮忙。

课程.rb

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

问题.rb

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

答案.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>

这是我在 head 标签结束之前添加的 javascript。我收到“$('form').on 不是函数的错误。(在 '$('form').on', '$('form'....

<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>

【问题讨论】:

  • 你的 jquery 版本是什么?
  • jQuery JavaScript 库 v1.4.3
  • 您至少需要 jquery 1.7 才能使用 .on 函数。此外,如果您不想升级 jquery,可以使用 .bind 代替 .on。
  • 谢谢让我试试
  • 更新了 Jquery 并感谢所有工作。请作为答案发送,我会标记它。

标签: javascript ruby-on-rails ruby-on-rails-3 nested-forms nested-attributes


【解决方案1】:

Jquery .on() 函数至少需要 jquery 1.7 才能工作。更新您的 jquery 或使用 .bind() 代替 .on()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多