Rails Simple_form_for hidden_field 值不正确
Rails Simple_form_for hidden_field Value Incorrect
我的 Rails 应用程序中有以下表格:
<%= simple_form_for(@ingredient) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
...
<%= f.hidden_field :recipe, value: @recipe.id %>
</div>
<div class="form-actions text-center">
<%= f.button :submit %>
</div>
<% end %>
我的控制器里有这个:
@recipe = Recipe.find(params[:id])
@ingredient = Ingredient.new
@ingredients = Ingredient.where(user_id: current_user.id, recipe_id: @recipe.id)
而且我已经用 byebug 测试了 @recipe.id
以数字形式出现(4
在我正在测试的实例中)。
当表单实际呈现时,值是正确的:
但是,当创建记录时,ingredient
仍然是使用 recipe_id
nil
创建的。关于出了什么问题的任何智慧?
如前所述,您应该使用 f.hidden_field :recipe_id
但我会加一个,试试这样使用:
成分控制器:
@ingredient = Ingredient.new({:recipe_id => @recipe.id})
在成分视图中:
f.hidden_field :recipe_id
这样您就不需要在视图中实例化配方。这也适用于您的编辑视图!它让事情变得清晰!
f.hidden_field :recipe
应该是f.hidden_field :recipe_id
我的 Rails 应用程序中有以下表格:
<%= simple_form_for(@ingredient) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
...
<%= f.hidden_field :recipe, value: @recipe.id %>
</div>
<div class="form-actions text-center">
<%= f.button :submit %>
</div>
<% end %>
我的控制器里有这个:
@recipe = Recipe.find(params[:id])
@ingredient = Ingredient.new
@ingredients = Ingredient.where(user_id: current_user.id, recipe_id: @recipe.id)
而且我已经用 byebug 测试了 @recipe.id
以数字形式出现(4
在我正在测试的实例中)。
当表单实际呈现时,值是正确的:
但是,当创建记录时,ingredient
仍然是使用 recipe_id
nil
创建的。关于出了什么问题的任何智慧?
如前所述,您应该使用 f.hidden_field :recipe_id
但我会加一个,试试这样使用:
成分控制器:
@ingredient = Ingredient.new({:recipe_id => @recipe.id})
在成分视图中:
f.hidden_field :recipe_id
这样您就不需要在视图中实例化配方。这也适用于您的编辑视图!它让事情变得清晰!
f.hidden_field :recipe
应该是f.hidden_field :recipe_id