如何创建一个将字段添加到导轨形式的按钮
我在Rails中使用fields_for
接受嵌套属性的表单:
<%= form_with(model: @combat_tracker, url: form_url) do |f| %>
…
<%= f.fields_for :zones do |zone| %>
<div class="zone-field">
<%= zone.text_field :name %>
<%= zone.check_box :_destroy %>
<%= zone.label :_destroy, "Remove zone" %>
</div>
<% end %>
…
<% end %>
当前,这为我提供了@combat_tracker
的任何现有ZONE
s on Input字段。我想添加一个按钮,该按钮将动态添加一个新的Zone-fields
DIV,以添加新的区域
在提交表单时要添加。
我正在使用Rails 7,并假设该解决方案将涉及使用涡轮或可能的刺激,但无法确定这样做的最佳方法。谢谢。
I have a form in Rails which uses fields_for
to accept nested attributes:
<%= form_with(model: @combat_tracker, url: form_url) do |f| %>
…
<%= f.fields_for :zones do |zone| %>
<div class="zone-field">
<%= zone.text_field :name %>
<%= zone.check_box :_destroy %>
<%= zone.label :_destroy, "Remove zone" %>
</div>
<% end %>
…
<% end %>
Currently this gives me input fields for any existing zone
s on @combat_tracker
. I want to add a button that will dynamically add a new zone-fields
div for a new zone
to be added when the form is submitted.
I’m using Rails 7 and assume the solution will involve the use of Turbo or possibly Stimulus, but can’t quite figure out the best way to do this. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您不需要涡轮或刺激。看看 Cocoon Gem ,它应该按照您的需要做。
在这里解释所有过程对我来说非常复杂,但是尝试关注本指南如果宝石的时间太长。
I don't think you need Turbo or Stimulus. Take a look at cocoon gem, it should do exactly what you're looking for.
Explaining all process here is quite complex for me, but try to follow this guide if the gem's one is too long.