如何隐藏表单字段并仍然提交所有字段?

时间:2012-11-02 12:22:53

标签: ruby-on-rails twitter-bootstrap erb

我有这个轨道部分:

<%= form_for @ptab, :remote => true, :html => { :class => 'form-horizontal' } do |f| %>
  <div class="control-group">
    <%= f.label :name, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :name, :class => 'text_field', :placeholder => 'Enter a name...' %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :address, :class => 'control-label' %>
    <div class="controls">
      <%= f.text_field :address, :class => 'text_field', :placeholder => 'Paste a link...' %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :sequence, :class => 'control-label' %>
    <div class="controls">
      <%= f.number_field :sequence, :class => 'number_field', :placeholder => 'Enter an order number...' %>
    </div>
  </div>
  <div class="control-group">
    <%= f.label :gtab_id, :class => 'control-label' %>
    <div class="controls">
      <%= f.number_field :gtab_id, :class => 'number_field' %>
    </div>
  </div>


    <div class="modal-footer">
                <%= f.submit nil, :class => 'btn btn-primary' %>
                <!-- <%= link_to t('.cancel', :default => t("helpers.links.cancel")), ptabs_path, :class => 'btn' %> --> <!-- this was the original cancel button -->
                <a data-dismiss="modal" class="btn" href="#">Close</a>
     </div>

<% end %>

我想将最后一个.control-group中的字段转换为隐藏字段,但我仍然希望将其提交给控制器。

我应该将其更改为:

<div class="control-group">
        <%= f.label :gtab_id, :class => 'control-label' %>
        <div class="controls">
          <%= f.number_field :gtab_id, :class => 'number_field' %>
        </div>
      </div>

为了使其不以渲染形式显示,但仍然以表格提交?

1 个答案:

答案 0 :(得分:2)

使用hidden_field?或者我错过了什么?

<%= f.hidden_field :gtab_id %>

当然要删除它周围的div以及label。 hidden_​​field作为常规字段提交,无后顾之忧