如何使用ruby on rails将下拉列表,其他选项文本输入数据存储在数据库中?

时间:2017-11-13 12:28:01

标签: ruby-on-rails

我确实有一个选择下拉列表,并且在那个少数选项中也在那个OTHERS选项中

<div class="form-group clearfix">
    <div class="col-sm-2" id="mainone">
        <%#= form.text_field :city, placeholder: "CITY", class: 'form-control required' %>
        <%= form.select :city, options_for_select([['DELHI'],['GURGAON'],['FARIDABAD'],['GHAZIABAD'],['NOIDA'],['MUMBAI'],['THANE'], ['BANGALORE'],['OTHERS']]), {include_blank: 'CITY*'}, class: 'form-control required', name: 'city', :onchange => 'Checkselectedone(this.value);' %>
            <% if @ezetab.errors[:city].present? %>
                <span class="error_msg"><%= @ezetab.errors[:city][0]%></span>
            <% end %>
    </div>
</div>
<div class="form-group clearfix">
    <div class="col-sm-2">
        <%= form.text_field :city, name: 'city', id: 'newtext', label: "Others",style: 'display:none;' %>
    </div>
</div>
  

当我选择其他选项时,应该再来一个文本框i   可以输入哪个是我的城市。

     

但事情就是这里输入的城市数据也应存入   相同的提交。但我不知道如何在同一领域保存那个。

     

这些是我的字段

def ezetab_params
        params.require(:ezetab).permit(:name, :email, :phonenumber, :organization, :city)
    end
  

这是javascript代码

<script type="text/javascript">
function Checkselectedone(val){
    alert("one more text box is coming");
 var element=document.getElementById('newtext');
 if(val=='OTHERS')
   element.style.display='block';
 else  
   element.style.display='none';
}

</script>

can any one tell me how to store that other option data also in the same city filed and i have give required also for that it should not be shown any validation msg. please let me know.
thanks in advance

1 个答案:

答案 0 :(得分:0)

<div class="form-group clearfix">
    <div class="col-sm-2" id="mainone">
        <%#= form.text_field :city, placeholder: "CITY", class: 'form-control required' %>
        <%= form.select :city, options_for_select([['DELHI'],['GURGAON'],['FARIDABAD'],['GHAZIABAD'],['NOIDA'],['MUMBAI'],['THANE'], ['BANGALORE'],['OTHERS']]), {include_blank: 'CITY*'}, class: 'form-control required', :onchange => 'Checkselectedone(this.value);' %>
            <% if @ezetab.errors[:city].present? %>
                <span class="error_msg"><%= @ezetab.errors[:city][0]%></span>
            <% end %>
    </div>
</div>
<div class="form-group clearfix">
    <div class="col-sm-2">
        <%= text_field_tag :others, "", class: 'form-control', id: 'newtext', style: 'display:none;' %>
    </div>
</div>

控制器: -

def ezetab_params
   ep = params.require(:ezetab).permit(:name, :email, :phonenumber, :organization, :city)
   ep[:city] = params[:others] if params[:others].present?
   ep            
end