【问题标题】:Rails 3 Cocoon link_to_add_association Renders Partial TwiceRails 3 Cocoon link_to_add_association 部分渲染两次
【发布时间】:2013-01-23 14:24:37
【问题描述】:

正如预期的那样,我的部分被渲染了两次而不是只渲染一次。有什么想法吗?

这是我的个人视图

<%= simple_nested_form_for(@person) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">

    <%= f.input :name %>

    <h3>Records by year</h3>

    <div id='records'>
      <%= f.simple_fields_for :records do |record| %>
        <%= render 'record_fields', :f => record %>
      <% end %>

      <div class='links'>
        <%= link_to_add_association 'New Record', f, :records, :class => 'btn' %>
      </div>
    </div>

  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

模型(移除了常量和验证等内容):

class Person < ActiveRecord::Base

    attr_accessible :name, :records_attributes

    has_many :records, :dependent => :destroy
    accepts_nested_attributes_for :records, :reject_if => :all_blank, :allow_destroy => true

end

class Record < ActiveRecord::Base

    attr_accessible :price, :status, :year, :person_id

    belongs_to :person
end

我的 _record_fields.html.erb 部分看起来像这样:

<div class='nested-fields well'>
    <%= f.input :price %>

    <%= f.input :year %>

    <%= f.input :status, :collection => record_statuses, :include_blank => false %>

    <%= link_to_remove_association "Remove", f %>
</div>

一个有趣的问题是,如果我更改生成部分的位置(所以“之后”而不是默认的“之前”link_to_add_association 链接),它会在按钮之后生成部分,但在 link_to_add_association 之前生成副本链接。

我能找到的唯一报告的类似问题是生产中的缓存。这发生在开发中,我的缓存已关闭(默认情况下)。

我错过了什么吗?有人可以帮忙吗?

编辑: 查看茧JavaScript,似乎单击事件被调用两次(用$('.add_fields').click()测试它,触发$('.add_fields').live('click',函数() {...} ) 两次。我仍然不知道为什么会发生这种情况。非常感谢输入。

编辑#2:

控制器:

  # GET /persons/new
  # GET /persons/new.json
  def new
    @person = Person.new
    # @person.records.build

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @person }
    end
  end

  # GET /persons/1/edit
  def edit
    @person = Person.find(params[:id])
  end

【问题讨论】:

  • 我们可以看看您的控制器操作代码吗?
  • 我意识到在回家的路上我忽略了它。道歉并补充。
  • 渲染整个页面的时候还有其他的内联js吗?并尝试使用 form_for 和 fields_for
  • 我已经尝试删除所有 JS(包括我在 application.js 中的所有内容)。它没有帮助。我只用 form_for 和 fields_for 对其进行了测试,但这似乎也没有任何作用。

标签: ruby-on-rails-3 nested-attributes cocoon-gem


【解决方案1】:

我遇到了同样的问题。 我的问题是我需要两次茧 javascript。

曾经在我的 application.js 中

// app/assets/javascripts/application.js
//= require cocoon

在我的应用程序布局中一次

/ app/views/layouts/application.html.haml
= javascript_include_tag :cocoon

从我的布局中删除包含标签后,它开始按预期工作

【讨论】:

  • 哇。我不敢相信是这样的。你是一个了不起的人!谢谢!
  • 谢谢,这很有用
【解决方案2】:

有同样的问题,发现在我的应用程序布局(app/views/layouts/application.html.erb)中我已经包含了应用程序,而在 application.js 中我已经包含了茧。当我认为在 cocoon 中包含 application.js 时,cocoon 会触发两次。

(与上面相同,但略有不同,因为我没有在 application.html.erb 中明确指定 cocoon,我指定了包含 cocoon 的 application.js)

【讨论】:

  • 这里有同样的问题。所以我在 application.html.erb 的末尾删除了&lt;%= javascript_include_tag 'application' %&gt;。但是这样js文件还是包含进去的?
【解决方案3】:

这种情况发生在从 application.js 开始时,您需要两次茧,通常是在您尝试将 jQuery 更改为 vanilla Javascript 时

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 2012-10-16
    • 2023-03-11
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多