【问题标题】:Nested field ruby嵌套字段红宝石
【发布时间】:2015-12-11 07:48:10
【问题描述】:

我对 ruby​​ on rails 框架非常陌生,我尝试建立一个食谱网站以了解有关此框架的更多信息。

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

这是文件:

食谱.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

单位.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

所以现在,当我尝试创建新配方时,出现错误“成分除外,字符串已给定”。 我知道这是因为我的新成分不是在我的 create 方法中创建的,但我真的不知道该怎么做。如果有人可以帮助我:)

【问题讨论】:

  • "render 'recipe_ingredient_field', f: task" - what is task` 这里是不是渲染时报错?
  • 如何。是的。它必须是 f:recipe_ingredient。我会在家里试试这个然后回到这里。谢谢塞尔吉奥!
  • 我修复了我的错误,将render 'recipe_ingredient_field', f: task by render 'recipe_ingredient_field', f: recipe_ingredient 但错误仍然存​​在:预期成分(#70329373917340),得到字符串(#70329368504180 ) 在行:@recipe = current_user.recipes.build(recipe_params)

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


【解决方案1】:

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

但是...如果您打算在添加 recipe_ingredient 记录时构建一个新的成分记录,那么您需要一个 fields_for 子句来构建成分记录并将其关联到您的 recipe_ingredient 字段。还要确保你的 recipe_ingredient 包含 Accept_nested_attributes_for :ingredients

【讨论】:

    猜你喜欢
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多