[英]Rails / Active Record has_many through association - fetching a record
[英]Unpermitted parameter error in creating new model with has_many through association in Rails
我正在用Rails編寫Recipe應用程序。 現在,我陷入了這個錯誤。 我花了一個星期的時間找到解決該問題的方法。
更新模型這是我的模型:
class Recipe < ActiveRecord::Base
has_many :ingredient_recipes
has_many :ingredients, :through => :ingredient_recipes
accepts_nested_attributes_for :ingredient_recipes
end
class Ingredient < ActiveRecord::Base
has_many :ingredient_recipes
has_many :recipes, :through => :ingredient_recipes
end
class IngredientRecipe < ActiveRecord::Base
belongs_to :recipe
belongs_to :ingredient
end
IngredientRecipe是一個聯接模型,用於存儲配方和配料的值。
在“新建”操作中,我要為該食譜創建新的食譜以及“配料”,並指定該配料的數量(數量是聯接表“配料食譜”中的附加屬性)。
當我單擊“提交”時,它只是創建一個新配方,而沒有在“聯接”表中創建記錄。
這是我的錯誤:
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"AbnDeDLDWq3lvy4pozvXStVy7NeOIUuv1DU1U2/2EJ6n
/jM6E1669hyB90733XTY9DGAvc2hbbDmbL6NwhqCUg==",
"recipe"=>{"rec_name"=>"j", "rec_description"=>"k",
"ingredient_recipe"=>{"ingredient"=>{"id"=>"1"},
"quantity"=>"1"}}, "commit"=>"Create Recipe"}
Unpermitted parameter: ingredient_recipe
(0.1ms) BEGIN
SQL (0.2ms) INSERT INTO `recipes` (`rec_name`, `rec_description`,
`created_at`, `updated_at`) VALUES ('j', 'k', '2015-06-25 21:48:09', '2015-06-25 21:48:09')
(1.1ms) COMMIT
Redirected to http://localhost:3000/recipes/50
Completed 302 Found in 6ms (ActiveRecord: 1.4ms)
更新的控制器我的配方控制器:更新的控制器
def new
@recipe = Recipe.new
3.times { @recipe.ingredient_recipes.build.build_ingredient }
end
def create
@recipe = Recipe.new(recipe_params)
respond_to do |format|
if @recipe.save
format.html { redirect_to @recipe, notice: 'Recipe was successfully created.' }
format.json { render :show, status: :created, location: @recipe }
else
format.html { render :new }
format.json { render json: @recipe.errors, status: :unprocessable_entity }
end
end
end
我的食譜視圖表格:
<%= form_for(@recipe) do |f| %>
<% if @recipe.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@recipe.errors.count, "error") %> prohibited this recipe from being saved:</h2>
<ul>
<% @recipe.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :rec_name %><br>
<%= f.text_field :rec_name %>
</div>
<div class="field">
<%= f.label :rec_description %><br>
<%= f.text_area :rec_description %>
</div>
<table>
<thead>
<tr>
<th>Ingredients</th>
<th>Unit</th>
<th>Quantity</th>
<th>New Quantity</th>
</tr>
</thead>
<tbody>
<%= f.fields_for :ingredient_recipes do |fi| %>
<%= fi.fields_for do |i| %>
<tr>
<td> <%=i.collection_select(:ingredient_id, Ingredient.all, :id, :ing_name) %></td>
<% end %>
<td> <%=fi.text_field :quantity %></td>
</tr>
<% end %>
</tbody>
</table>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
更新后沒有錯誤,但是這里是我提交創建操作時得到的參數:參數:{“ utf8” =>“✓”,“ authenticity_token” =>“ NCT4NgS6ZvbqpcS4 + CsoYI4pJurYzwsCyj5U6k3MTwSFqDpdLMX + XVFIPZP6P3P6P3P6P3P6P0P6P0P6P0P6P0P0P6P0P6P0P0P0P1E0 “ => {” rec_name“ =>” s“,” rec_description“ =>” s“,” ingredient_recipes_attributes“ => {” 0“ => {” ingredient_id“ =>” 2“,” quantity“ =>” 1 “},” 1“ => {” ingredient_id“ =>” 1“,” quantity“ =>” 1“},” 2“ => {” ingredient_id“ =>” 3“,” quantity“ =>” 1 “}}}, ”提交“=>”創建配方“}(0.6ms)BEGIN SQL(4.6ms)INSERT INTO recipes
( created_at
, updated_at
)VALUES( '2015年7月1日0時37分28秒',“2015年-07-01 00:37:28')(2.1ms)COMMIT重定向到http:// localhost:3000 / recipes / 65已完成302在24ms內找到(ActiveRecord:7.3ms)
從2015年6月30日17:37:28 -0700開始為127.0.0.1的GET“ / recipes / 65”,由RecipesController#show處理為HTML參數:{“ id” =>“ 65”}配方加載(4.7ms)選擇recipes
。*從recipes
選擇recipes
。 id
= 65 LIMIT 1在布局/應用程序中呈現的Recipes / show.html.erb(0.7ms)在136ms內完成200 OK(查看:126.4ms | ActiveRecord:4.7ms)
似乎一切都在正確的方向上,但是我不知道為什么在執行show show action之后它不顯示我剛剛添加的配方的屬性,並且它也沒有在聯接表中創建記錄。
請幫我! 非常感謝
class Ingredient < ActiveRecord::Base
has_many :ingredient_recipes, inverse_of: :ingredient_recipes, autosave: true
has_many :recipes, :through => :ingredient_recipes
以下行是不正確的。 與自身的逆關系無效:
has_many :ingredient_recipes, inverse_of: :ingredient_recipes, autosave: true
這就是為什么出現錯誤的原因:
不允許的參數:Ingredient_recipe
class IngredientRecipe < ActiveRecord::Base
belongs_to :recipe, :inverse_of ingredient_recipes
belongs_to :ingredient
accepts_nested_attributes_for :ingredient
end
您必須先成功創建父記錄,然后才能添加“屬於”子級。 您的自動保存參數可以執行此操作,但是由於某種原因它無法工作,您必須對其進行調試。 為此,我建議您在創建父配方記錄后在子表上顯式創建一個操作。
一旦使用顯式調用正確創建了父記錄和子記錄,然后將自動保存回模型中,回滾顯式創建的內容,並查看父存儲是否調用了子存儲。 我總是使用顯式保存,因為我想控制該過程。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.