嵌套字段ruby

时间:2015-12-11 07:48:11

标签: ruby-on-rails ruby ruby-on-rails-4 rubygems

我对rails框架上的ruby非常陌生,我尝试构建一个配方网站,以了解有关此框架的更多信息。

我有一个实体Recipe.rb,它有许多RecipeIngredient。 RecipeIngredient是与成分,单位和数量相关的实体。

以下是文件:

Recipe.rb

class Recipe < ActiveRecord::Base
  extend Enumerize, FriendlyId

  belongs_to :user
  has_many :directions
  has_many :recipe_ingredients
  has_many :ingredients, through: :recipe_ingredients
  accepts_nested_attributes_for :directions, reject_if: :all_blank,   allow_destroy: true
  accepts_nested_attributes_for :recipe_ingredients, reject_if: :all_blank, allow_destroy: true

  friendly_id :title, use: :slugged
  has_attached_file :image, styles: { thumb: "270x200#", main: "570x420#" },  default_url: "/images/:style/missing.png"
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/

  enumerize :difficulty, in: [:easy, :medium, :hard], default: :easy
end

RecipeIngredient.rb

class RecipeIngredient < ActiveRecord::Base
  belongs_to :ingredient
  belongs_to :unit
  belongs_to :recipe
end

Ingredient.rb

class Ingredient < ActiveRecord::Base
  belongs_to :recipe_ingredient
end

Unit.rb

class Unit < ActiveRecord::Base
  belongs_to :recipe_ingredient
end

如您所见,我使用gem Cocoon来处理嵌套字段。因此,当用户想要创建食谱时,他必须提供食谱配料。

我的_form看起来像这样:(我删除了所有的html标签(这里的缩进不好)

= simple_form_for @recipe, html: {multipart: true} do |f|
  - if @recipe.errors.any?
    .alert.alert-danger
      - @recipe.errors.full_messages.each do |msg|
        li= msg
        = f.input_field :title, label: false, placeholder: 'Nom de la recette', required: true
        = f.input_field :prepareTime, label: false,  placeholder: 'Temps de préparation (en mn)', required: true
        = f.input_field :cookingTime, label: false, placeholder: 'Temps de cuisson (en mn)', required: true
        = f.input_field :difficulty, label: false, required: true, placeholder: 'Difficulté'
        = f.input_field :serves, label: false, placeholder: 'Nombre de personnes', required: true
        = f.input_field :description, label: false, placeholder: 'Description', required: true
    = f.simple_fields_for :recipe_ingredients do |recipe_ingredient|
      = render 'recipe_ingredient_field', f: task
      = link_to_add_association 'Ajouter un ingrédient', f, :recipe_ingredients, class: 'add'

    = f.simple_fields_for :directions do |direction|
      = render 'direction_field', f: task
      = link_to_add_association 'Ajouter une étape', f, :directions, class: 'add'

      = f.input_field :image, as: :file, label: false
    = f.button :submit, value: 'Ajouter la recette', class: 'button'

_recipe_ingredient_fields.html.slim

.f-row.ingredient
  .nested_fields
    .large
      = f.input_field :ingredient, label: false, placeholder: "Nom de l'ingrédient"
    .small
      = f.input_field :quantity, label: false, placeholder: 'Quantité'
    .third.mb
      = f.association :unit, label: false
    = link_to_remove_association '-', f, class: 'remove'

最后但并非最不重要的是,我的食谱控制器

class RecipesController < ApplicationController
  before_action :authenticate_user!, only: [:new, :create]
  before_action :find_recipe, only: [:show]

  def index
    @recipes = Recipe.all
  end

  def new
    @recipe = current_user.recipes.build
  end

  def create
    @recipe = current_user.recipes.build(recipe_params)
    if @recipe.save
      redirect_to @recipe, notice: 'La recette a été ajoutée'
    else
      render 'new'
    end
  end

  def show
  end

  private

  def recipe_params
    params.require(:recipe).permit(:title, :prepareTime, :cookingTime, :difficulty, :serves, :description, :image, directions_attributes: [:id, :step, :done, :_destroy], recipe_ingredients_attributes: [:id, :ingredient, :unit, :quantity, :done, :_destroy])
  end

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

所以现在,当我创建一个新配方时,我有一个错误“成分除外,字符串给出”。 我知道这是因为我的新成分不是在我的创建方法中创建的,但我真的不知道如何做到这一点。如果有人可以帮助我:)

1 个答案:

答案 0 :(得分:0)

如果您想将recipe_ingredients中的成分保存为字符串,那么我认为您的问题是由模型与输入字段之间的名称冲突引起的,因此请将输入字段重命名为其他内容并更新您的强参数。

然而......如果你打算在添加recipe_ingredient记录时建立一个新的成分记录,那么你需要有一个fields_for子句来构建成分记录并将它与你的recipe_ingredient字段相关联。还要确保你的recipe_ingredient包含accepts_nested_attributes_for:ingredients