Ruby on rails 嵌套字段ruby
我对RubyonRails框架非常陌生,我尝试构建一个菜谱网站来了解更多关于这个框架的信息 我有一个实体Recipe.rb,它有许多接受成分。接受成分是与成分、单位和数量相关的实体 以下是文件: Recipe.rbRuby on rails 嵌套字段ruby,ruby-on-rails,ruby,ruby-on-rails-4,rubygems,Ruby On Rails,Ruby,Ruby On Rails 4,Rubygems,我对RubyonRails框架非常陌生,我尝试构建一个菜谱网站来了解更多关于这个框架的信息 我有一个实体Recipe.rb,它有许多接受成分。接受成分是与成分、单位和数量相关的实体 以下是文件: Recipe.rb class Recipe < ActiveRecord::Base extend Enumerize, FriendlyId belongs_to :user has_many :directions has_many :recipe_ingredients
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
class RecipeIngredient < ActiveRecord::Base
belongs_to :ingredient
belongs_to :unit
belongs_to :recipe
end
class Ingredient < ActiveRecord::Base
belongs_to :recipe_ingredient
end
class Unit < ActiveRecord::Base
belongs_to :recipe_ingredient
end
\u recipe\u component\u 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
class RecipesController
所以现在,当我创建一个新的配方时,我有一个错误“配料除外,字符串给定”。
我知道这是因为我的新配料不是在我的创建方法中创建的,但我真的不知道如何做到这一点。如果有人能帮助我:)如果您想将配方配料中的配料保存为字符串,那么我认为您的问题是由模型和输入字段之间的名称冲突引起的,因此请将您的输入字段重命名为其他名称并更新强参数
然而,如果您打算在添加配方配料记录时构建新的配料记录,则需要有一个字段for子句,以便构建配料记录并将其与配方配料字段关联。另外,请确保配方中的配料包含接受:配料的嵌套属性如果要将配方中的配料保存为字符串,则我认为您的问题是由模型和输入字段之间的名称冲突引起的,因此请将输入字段重命名为其他名称并更新强参数
然而,如果您打算在添加配方配料记录时构建新的配料记录,则需要有一个字段for子句,以便构建配料记录并将其与配方配料字段关联。还要确保配方成分包含以下内容的属性:“呈现配方成分字段”,f:task
”-这里是什么?这不是在渲染时引发错误吗?如何。对它必须是f:recipe_配料。我会在家里试试,然后回来。谢谢塞吉奥!我修复了我的错误,将呈现“配方成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分-这里的任务是什么?这不是在渲染时引发错误吗?如何。对它必须是f:recipe_配料。我会在家里试试,然后回来。谢谢塞吉奥!我修复了我的错误,将渲染“配方成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分成分
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