【问题标题】:Render nested form in bootstrap tab在引导选项卡中渲染嵌套表单
【发布时间】:2018-09-26 06:35:12
【问题描述】:

在 Rails 5 上工作, 我正在尝试在每个动态选项卡中呈现嵌套表单(相同模型), 但是现在所有选项卡中都呈现相同的格式,如何在不同的选项卡中呈现不同的字段集

views/material_masters/new.html.erb

<div class="col-md-12">
   <div class="panel panel-default">
     <div class="panel-body">
      <div class="tabbable-line">
        <ul class="nav nav-tabs ">
          <% @part_locations.each.with_index do |l, i| %>
          <li <%= 'class="active"' if i == 0 %>>
            <a href="#<%= l.location_name %>" data-toggle="tab"> <%= l.location_name %> </a>
          </li>
          <% end %>
          <li><button type="button" class="fa fa-plus btn btn-primary btn-xs" data-toggle="modal" data-target="#myModal"></button> </li>
        </ul>
        <div class="tab-content">
          <% @part_locations.each.with_index do |l, i| %>
          <div class="tab-pane <%= 'active' if  i == 0 %>" id="<%= l.location_name %>">
            <%= f.fields_for :material_locations do |builder| %>
            <%= render 'material_location_fields', :f => builder  %>
            <% end %>
            <%= link_to_add_association "Add", f, :material_locations, class: "btn btn-primary btn-xs"  %>
          </div>
          <% end %>
        </div>
      </div>

    </div>
  </div>
</div>

views/material_masters/_material_location_fields.html.erb

<fieldset>
  <div class= "nested-fields">
    <div class="col-sm-3">
      <%= f.label :material_location, "Material Location" %>
      <%= f.text_field :mat_location,class:"form-control",required: true %>
    </div>
    <div class="col-sm-3">
      <%= f.label :opening_stock  %>
      <%= f.text_field :opening_stock ,class:"form-control",onKeyPress:"return NumbersOnly(this, event,true)",required: true  %>
    </div>
    <div class="col-sm-3">
      <%= f.label :reorder_qty %>
      <%= f.text_field :reorder_qty, class:"form-control",onKeyPress:"return NumbersOnly(this, event,true)",required:true  %>
    </div>
  </div>
</fieldset>

model/material_master.rb

class MaterialMaster < ApplicationRecord
   has_many :material_locations
   accepts_nested_attributes_for :material_locations,allow_destroy: true
 end

https://i.stack.imgur.com/b6Ssr.png

https://i.stack.imgur.com/69h3X.png

听说我正在尝试在班加罗尔选项卡中添加位置,我正在添加孟买位置,在德里选项卡中,我想再添加一个材料位置,但下一个选项卡中会出现相同的表单

【问题讨论】:

  • 请格式化您的代码,很难理解出了什么问题。
  • 你能给我们看看html源码吗?
  • 我已经编辑了我的问题并添加了相关图片
  • 我正在尝试将索引值传递给字段如何将索引值传递给每个字段

标签: javascript jquery ruby-on-rails ruby


【解决方案1】:

您的代码显然是在一行中有空格:

<a href="#<%= l  .location_name %>" data-toggle="tab"> <%= l.location_name %> </a>

你可以注意到 l .location_name 你可以删除空格吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2014-12-19
    • 2018-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多