繁体   English   中英

如何使用Rails在数据库中存储下拉列表和其他选项文本输入数据?

[英]How to store the dropdown, other option text input data in database using ruby on rails?

我确实有一个选择下拉列表,在那几个选项中,在那个其他选项中,

<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>

当我选择其他选项时,应该会出现另一个文本框,然后我可以输入哪个城市。

但是这里要输入的城市数据也应该存储在同一文件中。 但我不知道如何将其保存在同一字段中。

这些是我的领域

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
<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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM