加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – Rails 3 Cocoon link_to_add_association渲

发布时间:2020-12-17 04:17:14 所属栏目:百科 来源:网络整理
导读:正如预期的那样,我的部分渲染两次而不是一次.有什么想法吗? 这是我的人物观点 %= simple_nested_form_for(@person) do |f| % %= f.error_notification % div class="form-inputs" %= f.input :name % h3Records by year/h3 div id='records' %= f.simple_fi
正如预期的那样,我的部分渲染两次而不是一次.有什么想法吗?

这是我的人物观点

<%= 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链接之前生成副本.

我在这里报告的唯一类似问题是生产中的缓存.这在开发中发生,我的缓存被关闭(默认情况下).

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

编辑:
看一下cocoon JavaScript,似乎点击事件被一次点击两次调用(用$(‘.add_fields’)测试.单击(),触发$(‘.add_fields’).live(‘click’,function (){…})两次.我仍然不知道为什么会发生这种情况.输入完全??赞赏.

编辑#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

解决方法

我遇到了同样的问题.
我的问题是我需要两次cocoon javascript.

进入我的application.js

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

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

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读