添加遥控器后按钮不会提交发布请求:在轨道中为 true)



我正在我的铁路应用程序中构建一个嵌套表单,用于资源recipe,具有许多ingredients。我在表单上包含了一个带有remote: true的链接,该链接添加了通过jQuery/ajax脚本控制的其他ingredients字段。但是,现在我的表格上的按钮不会提交邮政请求。

这是我的形式:

<%= form_for @recipe do |f| %>
<h1>
    <%= f.label :name %><br>
    <%= f.text_field :name %><br>
</h1>
<%= f.fields_for :ingredients do |p| %>
    <%= render 'ingredient_form', f: p %>
    <%= link_to "Add ingredient", recipe_new_path, id: "new_ingredient", remote: true %>
<% end %>
<h1>
    <%= f.label :steps %><br>
    <%= f.text_area :steps %><br>
</h1>
<%= f.submit "Add" %><br>
<% end %>
... _ingredient_form
<%= form_for :ingredients do |p| %>
<table>
    <tr>
        <td>
            <%= p.label :ing %><br>
            <%= p.text_area :ing %><br>
        </td>
        <td>
            <%= p.label :amount %><br>
            <%= p.text_area :amount %><br>
        </td>
    </tr>
</table>

食谱控制器:

def new
  @recipe = Recipe.new  
  @ingredient = @recipe.ingredients.build
end
def create
  @recipe = Recipe.create!(recipe_params)
  redirect_to recipe_path(@recipe)
end

new.js.erb:

$("#new_ingredient").before("<%= j render 'ingredient_form', f: p %>");

链接到github repo:

https://github.com/jlcampbell1991/recipe-box

,因为我在我的部分中使用了 form_for而不是 field_for,所以表单在表单元素的外部呈现按钮。

最新更新