Rails:在弹出值时隐藏表单字段

时间:2016-12-14 23:14:55

标签: ruby-on-rails ruby-on-rails-4 form-for

我有这个表格。当对象具有性别值时,我想将性别字段更改为隐藏字段,因此用户无需查看它。这样做的最佳方式是什么?

    <%= form_for( object ,:html => {:class => "form-horizontal"}) do |f| %> 
       <div class="form-group row">
          <%= f.label(:first_name, :class => "control-label col-xs-4 input-lg") %>

            <div class="form-inline col-xs-8">
                <div class="">
                    <%= f.text_field( :first_name,{class: "form-control input-lg"}) %>
                </div>
            </div>
        </div>

        <div class="form-group row">
          <%= f.label(:last_name, :class => "control-label col-xs-4 input-lg") %>

            <div class="form-inline col-xs-8">
                <div class="">
                    <%= f.text_field( :last_name,{class: "form-control input-lg"}) %>
                </div>
            </div>
        </div>
     <div class="form-group row">
          <%= f.label(:gender, :class => "control-label col-xs-4 input-lg") %>
            <div class="col-xs-8">
            <%= f.select( :gender, Dropdown.gender,{:prompt =>"Please Choose"},{class: "form-control input-lg"}) %>
            </div>
        </div>
            <div class="col-xs-8 col-xs-offset-4">
               <%= f.submit object.submit_button_name, {class: 'btn btn-lg'} %>
            </div>

    <% end %>

2 个答案:

答案 0 :(得分:1)

您可以简单地使用if语句根据是否设置性别来呈现不同的字段类型:

<form>
    <table>
        <tr>
            <th scope="row">Location:</th>
            <td><select><option>Hello</option></select></td>
        </tr>
        <tr>    
            <th scope="row">Name:</th>
            <td><input name="example" /></td>
        </tr>
    </table>
</form>

答案 1 :(得分:0)

不要隐藏它,最好不要渲染它。试试这样:

<% unless object.gender != nil %>
<div class="form-group row">
  <%= f.label(:gender, :class => "control-label col-xs-4 input-lg") %>
<div class="col-xs-8">
  <%= f.select( :gender, Dropdown.gender,{:prompt =>"Please Choose"},{class: "form-control input-lg"}) %>
 </div>
 </div>
<% end %>