小编Bri*_*ect的帖子

创建新配方时,cocoon gem属性不会保存

在mackenziechild-recipe_box的第3周,我遇到了一些关于Cocoon的问题.我已经设计安装,以及和我ingredientsdirections属性,当我创建一个新的未保存Recipe.但是当我更新现有产品时Recipe,一切都很好.错误消息是:

配料配方必须存在,方向配方必须存在

我究竟做错了什么?我正在使用rails 5

应用程序/模型/ recipe.rb

class Recipe < ApplicationRecord
  validates :title, :description, :image, presence: true
  has_attached_file :image, styles: { medium: "400x400#" }
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
  belongs_to :user
  has_many :ingredients
  has_many :directions
  accepts_nested_attributes_for :ingredients, :reject_if => :all_blank, :allow_destroy => true
  accepts_nested_attributes_for :directions, :reject_if => :all_blank, :allow_destroy => true
end
Run Code Online (Sandbox Code Playgroud)

应用程序/控制器/ recipes_controller.rb

def new
  @recipe = Recipe.new
  @recipe = current_user.recipes.build
end

def create
  @recipe = Recipe.new(recipe_params)
  @recipe = current_user.recipes.build(recipe_params)
  if @recipe.save
    # …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails cocoon-gem

1
推荐指数
1
解决办法
896
查看次数

标签 统计

cocoon-gem ×1

ruby ×1

ruby-on-rails ×1