【发布时间】: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 istask` 这里是不是渲染时报错? -
如何。是的。它必须是 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