前提・実現したいこと
Strong Parametersを用いて、レコードを登録する際に以下のエラーが出ます。
NameError (undefined local variable or method 'ingredient_relation_params' for #<IngredientRelationsController:0x00007fb331f8a750>):
以下が現在作成中のレシピアプリのER図です。
以下の流れでレシピデータを登録しようとしています。
0. 入力フォームよりRecipesのデータを登録する
0. セッションにレシピIDを保存する
0. IngredientRelationsのデータ入力画面にリダイレクトする
0. 入力フォームよりIngredientRelationsのデータを登録する
0. セッションからレシピIDを取り出し、先ほど保存したレシピインスタンスを作成する
0. レシピインスタンスとパラメータで受け取ったIngredientRelationsのデータを関連づけて保存する
手順6でNameError (undefined local variable or method 'ingredient_relation_params' for #<IngredientRelationsController:0x00007fb331f8a750>):
が発生します。
model
ruby
1class IngredientRelation < ApplicationRecord 2 belongs_to :recipe 3 belongs_to :ingredient 4 validates :ingredient_id, presence: true 5 validates :recipe_id, presence: true 6 validates :weight, presence: true 7end 8 9class Recipe < ApplicationRecord 10 has_many :users, through: :myrecipes 11 has_many :directions, -> { order("number ASC") }, dependent: :destroy 12 accepts_nested_attributes_for :directions 13 has_many :ingredient_relations, dependent: :destroy 14 has_many :ingredients, through: :ingredient_relations 15 belongs_to :user 16end 17 18class Ingredient < ApplicationRecord 19 has_many :ingredient_relations, dependent: :destroy 20 has_many :recipes, through: :ingredient_relations 21end
View
html
1<% provide(:title, 'Create Recipe') %> 2<h1>レシピを作成する</h1> 3 4<h2>New Recipe</h2> 5<%= form_with model: @recipe do |f| %> 6 <%= f.label :name %> 7 <%= f.text_field :name, class: 'form-control' %> 8 <%= f.label :description %> 9 <%= f.text_field :description, class: 'form-control' %> 10<%= f.submit %> 11<% end %> 12 13<% provide(:title, '材料の登録') %> 14<h1>材料を登録する</h1> 15 16<%= form_with(model: @ingredient_relation, url: ingredient_relations_path ) do |f| %> 17重さ 18<%= f.number_field :weight %> 19重さの表示 20<%= f.text_field :display_weight_name %> 21材料 22<%= f.number_field :ingredient_id %> 23材料の表示名 24<%= f.text_field :display_ingredient_name %> 25<%= f.submit %> 26<% end %> 27
Controller
ruby
1 2class RecipesController < ApplicationController 3 4 def new 5 @recipe = Recipe.new 6 end 7 8 def create 9 @recipe = current_user.recipes.build(recipe_params) 10 @recipe.save! 11 session[:recipe_id] = @recipe.id 12 redirect_to new_ingredient_relation_path 13 end 14 15 private 16 17 def recipe_params 18 params.require(:recipe).permit(:name, :description) 19 end 20end 21 22class IngredientRelationsController < ApplicationController 23 24 def create 25 @recipe = Recipe.find_by(id: session[:recipe_id]) 26 @ingredient_relation = @recipe.ingredient_relations.build(ingredient_relation_params) 27 @ingredient_relation.save 28 redirect_to root_url 29 end 30 31 private 32 33 def ingreadint_relation_params 34 params.permit(:ingredient_id, :weight, :display_weight_name, :display_ingredient_name) 35 end 36 37end
環境
- Docker version 18.09.2
- docker-compose version 1.23.2
- ruby 2.5.5
- Rails 5.2.3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/27 06:11