代码之家  ›  专栏  ›  技术社区  ›  Liz

Rails Simple\u form\u for hidden\u字段值不正确

  •  0
  • Liz  · 技术社区  · 9 年前

       <%= 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 在我正在测试的实例中)。

    实际渲染表单时,值是正确的: enter image description here

    但是,在创建记录时 ingredient still是用 recipe_id 属于 nil

    2 回复  |  直到 9 年前
        1
  •  1
  •   MrYoshiji    9 年前

    这个 f.hidden_field :recipe f.hidden_field :recipe_id

        2
  •  0
  •   Pedro Gabriel Lima    9 年前

    如前所述,您应该使用f.hidden\u字段:recipe\u id

    配料控制器:

    @ingredient = Ingredient.new({:recipe_id => @recipe.id})
    

    在成分视图中:

    f.hidden_field :recipe_id