[英]Ruby on Rails Form Add an "Other" Option with Text Box to Select Dropdown
[英]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.