我正在寻找相当一段时间的答案,但在其他情况下解决的解决方案在我的案例中不起作用。
问题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 我也有保存它的问题。 即使我为成分和方向填写了多个inupts,我也会得到消息
配料食谱必须存在
这对我来说是一个巨大的阻碍点所以请帮忙。
答案 0 :(得分:1)
如documentation中所述,使用rails 5时,您应该以不同的方式编写关联:
has_many :ingredients, inverse_of: :recipe
简而言之:因为rails 5默认需要belongs_to关系。虽然这绝对有意义,但这也意味着必须更明确地宣布关联。保存嵌套项时,从理论上讲,父级尚未保存在验证中,因此rails需要帮助才能知道关系之间的链接。
第一个错误听起来像是一个turbolinks错误:不知怎的,你多次包含application.js
或cocoon.js
(例如,如果你把它放在页面的底部 - 从你显示的代码中看不到,但这可以解释它)。 Turbolinks将取代整个主体内容而不是头部,因此身体中的任何javascript都会被多次加载(导航,跟随链接),直到您实际刷新页面。