导轨茧宝石生产重复

问题描述:

我正在寻找相当一段时间的答案,但在其他情况下制定出的解决方案不工作在我的情况。导轨茧宝石生产重复

问题1: 当点击按钮 “Dodaj skladnik” 或 “Dodaj KROK” 我得到多个字段,而不是一个(有时2有时3有时甚至40)

我花到目前为止3小时试图解决这一点,但我不知道为什么是这样工作的

当我按Ctrl +它的工作原理F5只是一个时间....如果我再次尝试使用表格或重装问题仍然存在

我没有重复的coccon itse条目如果。

[email protected] /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 另外我有保存它的问题。 甚至当我填写的成分和方向的多个inupts我得到消息

Ingredients recipe must exist

这是巨大的堵点对我来说,现在,请帮助。

正如documentation解释,使用轨道5时,你应该写你的公会是不同的:

has_many :ingredients, inverse_of: :recipe 

总之:由于轨道5 belongs_to的关系是必需的默认值。虽然这绝对有意义,但这也意味着必须更明确地声明关联。当保存嵌套项目时,理论上父项还没有保存在验证中,因此导轨需要帮助来了解关系之间的关系。

第一个错误听起来像一个turbolinks错误:不知何故,您多次包含application.jscocoon.js(例如,如果您将它放在页面的底部 - 不显示您显示的代码,但这可以解释它) 。Turbolinks将取代整个身体内容而不是头部,因此身体中的任何JavaScript都将被多次加载(导航,下面的链接),直到您实际刷新页面。

+0

关于我的第一个问题,我发现问题出在%标题后缺少标签,因此js已初始化为标题 – MajQel