我正在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是一种连接模型,用于存储配方和成分的值。
In&#39; New&#39;动作,我想创建新食谱以及该食谱的成分,并指定该成分的数量(数量是连接表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 %>
我更新后没有错误,但这是我提交创建操作时得到的参数:
参数:{&#34; utf8&#34; =&gt;&#34;✓&#34;,&#34; authenticity_token&#34; =&gt;&#34; NCT4NgS6ZvbqpcS4 + CsoYI4pJurYzwsCyj5U6k3MTwSFqDpdLMX + XVFIpAP / pXD3ZDNr6T6mIOf58gUk6SEqOQ ==&# 34;,&#34;食谱&#34; =&gt; {&#34; rec_name&#34; =&gt;&#34; s&#34;,&#34; rec_description&#34; =&gt;&#34; s&#34;,&#34; ingredient_recipes_attributes&#34; =&gt; {&#34; 0&#34; =&gt; {&#34; ingredient_id&#34; =&gt;&#34; 2&#34;,& #34;数量&#34; =&gt;&#34; 1&#34;},&#34; 1&#34; =&gt; {&#34; ingredient_id&#34; =&gt;&#34; 1&#34 ;,&#34;数量&#34; =&gt;&#34; 1&#34;},&#34; 2&#34; =&gt; {&#34; ingredient_id&#34; =&gt;&#34; 3&#34;,&#34;数量&#34; =&gt;&#34; 1&#34;}}},&#34;提交&#34; =&gt;&#34;创建食谱&#34;}
(0.6ms)BEGIN
SQL(4.6ms)INSERT INTO recipes
(created_at
,updated_at
)VALUES(&#39; 2015-07-01 00:37:28&#39;,&#39; 2015 -07-01 00:37:28&#39;)
(2.1ms)COMMIT
重定向到http://localhost:3000/recipes/65
完成302发现在24ms(ActiveRecord:7.3ms)
开始GET&#34; / recipes / 65&#34;在2015-06-30 17:37:28 -0700为127.0.0.1
RecipesController处理#show as HTML
参数:{&#34; id&#34; =&gt;&#34; 65&#34;}
配方加载(4.7ms)SELECT recipes
。* FROM recipes
WHERE recipes
。id
= 65 LIMIT 1
在布局/应用程序中呈现recipes / show.html.erb(0.7ms)
在136ms完成200 OK(浏览次数:126.4ms | ActiveRecord:4.7ms)
似乎一切都在正确的方式,但我不知道为什么在渲染展示动作后它没有显示我刚刚添加的配方的属性,它也没有创建在连接表中记录。
请帮帮我!非常感谢
答案 0 :(得分:1)
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
在添加“属于”子项之前,必须先成功创建父记录。您的自动保存参数应该执行此操作,但由于某些原因它无法运行,您必须调试。为此,我建议您在创建父配方记录后在子表上显式创建操作。
使用显式调用正确创建父记录和子记录后,将自动保存放回模型中,回滚显式创建并查看父保存是否调用子保存。我总是使用显式保存,因为我想控制进程。