Rails 茧 gem 产生重复

Rails cocoon gem producing duplicates

我正在为此寻找相当长一段时间的答案,但在其他情况下有效的解决方案不适用于我的情况。

问题 1: 单击按钮 "Dodaj skladnik" 或 "Dodaj krok" 时 我得到多个字段而不是一个(有时 2 有时 3 有时甚至 40)

到目前为止,我花了 3 个小时试图解决这个问题,但我不知道为什么会这样

当我按 ctrl+f5 时它起作用了,但只有一次....如果我再次尝试使用表单或重新加载问题仍然出现

我没有 coccon 本身的重复条目。

   amon@AMUNET /app/amanda $ grep -rnw . -e 'cocoon'
Plik binarny ./tmp/cache/assets/sprockets/v3.0/7e/7e5wAAtpwJITUTZozWduRq866c-VsC2gAZvJOcVZmPo.cache pasuje do wzorca
Plik binarny ./tmp/cache/assets/sprockets/v3.0/0l/0l98NX_hEbCP2xKSXIBRKAyLnEDTcUK6gyw5MyNYbPo.cache pasuje do wzorca
Plik binarny ./tmp/cache/assets/sprockets/v3.0/rt/rtICrPE1OuOezvTCGzwYnqRsSHfhoYmMhu8dQSfP-is.cache pasuje do wzorca
./tmp/cache/assets/sprockets/v3.0/V1/V1yaq6iLPp3CDZSltLooShZ6SFBM5vWEjQhQSmoNWEU.cache:3:I"environment-version:ETTI"environment-paths;TTI"rails-env;TTI"Zprocessors:type=application/javascript&file_type=application/javascript&pipeline=self;TTI"{file-digest:///home/amon/.rbenv/versions/2.4.0/lib/ruby/gems/2.4.0/gems/cocoon-1.2.10/app/assets/javascripts/cocoon.js;TTF
./app/assets/javascripts/application.js:13://= require cocoon
./Gemfile:37:gem 'cocoon', '~> 1.2', '>= 1.2.10'
./Gemfile.lock:50:    cocoon (1.2.10)
./Gemfile.lock:183:  cocoon (~> 1.2, >= 1.2.10)

部分代码: application.js

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require cocoon
//= require_tree .

recipes_controller.rb

    class RecipesController < ApplicationController
        before_action :find_recipe, only: [:show, :edit, :update, :destroy]

        def index
            @recipe = Recipe.all.order("created_at DESC")
        end

        def show

        end

        def new
            @recipe = Recipe.new
        end

        def create
            @recipe = Recipe.new(recipe_params)

            if @recipe.save
                redirect_to @recipe, notice: "Przepis zostal dodany"
            else
                render 'new'
            end
        end

        def edit
        end

        def update
            if @recipe.update(recipe_params)
                redirect_to @recipe
            else
                render 'edit'
            end
        end

        def destroy
            @recipe.destroy
            redirect_to root_path, notice: "Przepis zostal usuniety"
        end


        private

        def recipe_params
            params.require(:recipe).permit(:tittle, :description, :image, ingredients_attributes: [:id, :name, :_destroy], directions_attributes: [:id, :step, :_destroy]  )
        end

        def find_recipe
            @recipe = Recipe.find(params[:id])
        end
end

_form.html.haml

=simple_form_for @recipe, html: {multipart: true} do |f|
- if @recipe.errors.any?
    #errors
        %p
        = @recipe.errors.count
        Prevent this recipe from saving
    %ul
        - @recipe.errors.full_messages.each do |msg|
            %li= msg
.panel-body
    = f.input :tittle, input_html: { class: 'form-control' }
    = f.input :description, input_html: { class: 'form-control' }
    = f.input :image, input_html: { class: 'form-control' }
    .row
        .col-md-6
            %h3 Skladniki
            #ingredients
                = f.simple_fields_for :ingredients do |ingredient|
                    = render 'ingredient_fields', f: ingredient
                .links
                    = link_to_add_association 'Dodaj skladnik', f, :ingredients, class: "btn btn-default add-button"
        .col-md-6
            %h3 Wskazowki
            #directions
                = f.simple_fields_for :directions do |direction|
                    = render 'direction_fields', f: direction
                .links
                    = link_to_add_association 'Dodaj krok', f, :directions, class: "btn btn-default add-button"


    = f.button :submit, class: "btn btn-primary"

application.html.haml

    !!! 5
%html
%head
    %title Amanda project
    = stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true
    = javascript_include_tag 'application', 'data-turbolinks-track' => true
    = csrf_meta_tags

%body
    %nav.navbar.navbar-default
        .container
            .navbar-brand= link_to "Amanda project", root_path
            %ul.nav.navbar-nav.navbar-right
                %li= link_to "Dodaj nowy przepis", new_recipe_path
    .container
        - flash.each do |name, msg|
            = content_tag :div, msg, class: "alert"
    = yield

_ingredient_fields.html.haml

.form-inline.clearfix
.nested-fields
    = f.input :name, input_html: { class: "form-input form-control" }
    = link_to_remove_association "Usun", f, class: "form-button btn btn-default"

recipe.rb (型号)

class Recipe < ApplicationRecord
has_many :ingredients
has_many :directions

accepts_nested_attributes_for :ingredients,
                                reject_if: proc { |attributes| attributes['name'].blank? },
                                allow_destroy: true
accepts_nested_attributes_for :directions,
                                reject_if: proc { |attributes| attributes['step'].blank? },
                                allow_destroy: true

validates :tittle, :description, :image, presence: true
has_attached_file :image, styles: { :medium => "400x400#" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end

问题 2 我也有保存它的问题。 即使我填写了多个成分和说明信息,我也会收到消息

Ingredients recipe must exist

现在对我来说这是一个巨大的障碍,所以请帮忙。

documentation 中所述,当使用 rails 5 时,您应该以不同的方式编写关联:

has_many :ingredients, inverse_of: :recipe

简而言之:因为 rails 5 默认需要 belongs_to 关系。虽然这绝对有道理,但这也意味着必须更明确地声明关联。保存嵌套项时,理论上父项在验证时尚未保存,因此 rails 需要帮助了解关系之间的 link。

第一个错误听起来像 turbolinks 错误:不知何故你多次包含 application.jscocoon.js(例如,如果你将它放在页面底部 - - 从您显示的代码中看不到,但这可以解释)。 Turbolinks 将替换完整的正文内容而不是头部,因此正文中的任何 javascript 将被多次加载(导航,跟随 links),直到您真正刷新页面。