Ruby on rails Rails-Cocoon/简单表单嵌套字段未显示
我试图在配方书的编辑/创建部分为配料、说明和餐具创建嵌套字段,但只显示字段的大纲,没有任何嵌套位 型号Ruby on rails Rails-Cocoon/简单表单嵌套字段未显示,ruby-on-rails,simple-form,cocoon-gem,Ruby On Rails,Simple Form,Cocoon Gem,我试图在配方书的编辑/创建部分为配料、说明和餐具创建嵌套字段,但只显示字段的大纲,没有任何嵌套位 型号 class Recipe < ApplicationRecord has_many :ingredients has_many :directions has_many :utensils accepts_nested_attributes_for :ingredients, reject_if: proc { |attributes| att
class Recipe < ApplicationRecord
has_many :ingredients
has_many :directions
has_many :utensils
accepts_nested_attributes_for :ingredients, reject_if: proc { |attributes|
attributes['name'].blank? }, allow_destroy: true
accepts_nested_attributes_for :directions, reject_if: proc { |attributes|
attributes['step'].blank? }, allow_destroy: true
accepts_nested_attributes_for :utensils, reject_if: proc { |attributes|
attributes['name'].blank? }, allow_destroy: true
end
class Ingredient < ApplicationRecord
belongs_to :recipe
end
class Direction < ApplicationRecord
belongs_to :recipe
end
class Utensil < ApplicationRecord
belongs_to :recipe
end
def recipe_params
params.require(:recipe).permit(:title, :description, :image,
ingredients_attributes: [:id, :name, :_destroy], directions_attributes:
[:id, :step, :_destroy], utensils_attributes: [:id, :name, :_destroy])
end
<div class="form-inline.clearfix">
<div class="nested-fields">
<%= f.input :step, input_html: { class: "form-input form-control" } %>
<%= link_to_remove_association "Remove", f, class: "form-button btn btn-default" %>
</div>
</div>
查看
<div class="row">
<div class="col-md-4">
<h3>Ingredients</h3>
<div id="ingredients">
<%= f.simple_fields_for :ingredients do |ingredient| %>
<%= render "ingredients_fields", f: ingredient %>
<div class="links">
<%= link_to_add_association "Add Ingredient", f, :ingredients, class: "btn btn-default add-button" %>
</div>
<% end %>
</div>
</div>
<div class="col-md-4">
<h3>Directions</h3>
<div id="directions">
<%= f.simple_fields_for :directions do |direction| %>
<%= render "directions_fields", f: direction %>
<div class="links">
<%= link_to_add_association "Add Step", f, :directions, class: "btn btn-default add-button" %>
</div>
<% end %>
</div>
</div>
<div class="col-md-4">
<h3>Utensils</h3>
<div id="utensils">
<%= f.simple_fields_for :utensils do |utensil| %>
<%= render "utensils_fields", f: utensil %>
<div class="links">
<%= link_to_add_association "Add Utensil", f, :utensils, class: "btn btn-default add-button" %>
</div>
<% end %>
</div>
</div>
</div>
<%= f.button :submit, class: "btn btn-primary" %>
</div>
成分
方向
用具
局部(用于方向)
餐具的轮廓也没有出现,我不知道为什么
下面是它的样子:
任何帮助都将不胜感激,谢谢 是否有嵌套项 因为如果没有,它将解释行为:你的
链接到添加关联在简单字段中,因此它将为每个嵌套项显示,但如果没有,它也永远不会显示(注意:这可能是期望的行为,但我猜在大多数情况下不是)
cocoon文档中的所有示例都将link\u to\u add\u关联
放在simple\u字段
循环之外。如果您在阅读haml时遇到问题,请检查。cocoon gem需要正确的html格式才能工作,我认为您是html造成了问题,请尝试注释行和列代码,并以简单格式运行我注释了列和行,但这似乎没有什么区别,框中没有显示任何内容,餐具框也不存在。您是否在控制器方法中建立了关联,如@recipe.components.build
?,我认为这个问题与HTML结构有关,首先尝试使用简单的UI实现,而不使用引导